Custom IsValid data annotations are never called. (ASP.NET MVC 2.NET 4)

I have a custom data validation attribute that I created to make sure that the user input passwords are the same, but IsValid is never called.

User attribute:

public class IsSameAsAttribute : ValidationAttribute { public String TargetProperty { get; set; } private readonly object _typeId = new object(); public IsSameAsAttribute(string targetProperty) { TargetProperty = targetProperty; } public override bool IsValid(object value) { return false; //Type objectType = value.GetType(); //bool isValid = false; //PropertyInfo[] neededProperties = // objectType.GetProperties().Where(propertyInfo => propertyInfo.Name == TargetProperty).ToArray(); //return isValid; } public override object TypeId { get { return _typeId; } } } 

The data model applies to:

 public class RegistrationData { [Required(ErrorMessage = "First Name Required")] [StringLength(100, ErrorMessage = "First Name must be 100 characters or less.")] public String FirstName { get; set;} [Required(ErrorMessage = "Last Name Required")] [StringLength(100, ErrorMessage = "Last Name must be 100 characters or less.")] public String LastName { get; set; } [Required(ErrorMessage = "Email is Required")] [StringLength(200, ErrorMessage = "Email must be 200 characters or less.")] [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Valid Email Address is required.")] public String Email { get; set; } [Required(ErrorMessage = "Password is required")] public String Password { get; set; } [IsSameAs("Password")] public String PasswordRepeat { get; set; } [Required(ErrorMessage = "Division is required")] public String Division { get; set; } } 

And the controller from which it is called:

 [HttpPost] public ActionResult ValidationDemo(RegistrationData model) { if (ModelState.IsValid) { return Redirect("/"); } return View(model); } 

All checks out of the box work correctly, it's just my custom one that is not called. When debugging, I find that it is created when the constructor is called, but the set of interrupts never gets to IsValid.

What is happening and how to fix it?

UPDATE

All I mumbled, and if I call TryUpdateModel (model) in my controller, it finally calls IsValid. Thus, this means that my user attribute is not getting "registered" so that checks are performed in MVC 2. Is there a way to solve this problem?

 [HttpPost] public ActionResult ValidationDemo(RegistrationData model) { TryValidateModel(model); // <--- *** Added this line and it "works" if (ModelState.IsValid) { return Redirect("/"); } return View(model); } 
+7
c # validation asp.net-mvc-2 data-annotations
source share
2 answers

I see a similar / same / MVC2-related problem - class-level checking is called only if all properties are checked correctly.

Thus, several properties may fail, and all validation messages are added to the model state (and displayed on the client). But the class-level verification message is not one of them; IsValid is only called on them if the properties pass. Bit a bummer in terms of usability, but it works.

I do not call TryValidateModel ().

+3
source share

Have you tried to fill in all the other fields so that all validation checks are satisfied and find out if your custom check is activated? If client validation fails, the form will be prevented after publication, and therefore your custom server-side validation will not be submitted by the trigger,

For your custom validation attribute to work on the client side, you need to do some extra work.

Read on Phil's excellent checkout post here: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

0
source share

All Articles