Validate a property if another property matters to MVC 3 / jQuery Validator

How can I use Required Validation in the Prop2 property only if Prop1 is true?

Ex: public bool Prop1 { get; set; } [Required] // I need this validation only if the Prop1 is true. public string Prop2 { get; set; } 

Any idea? I need client and server side. Thanks

+5
jquery-validate validation asp.net-mvc asp.net-mvc-3
source share
3 answers

You can write your own validator for this task.

Let me know if you need help to do this.

0
source share

You can use MVC FoolProof Framework Checks

It has a useful feature like

 [RequiredIf] [RequiredIfNot] [RequiredIfTrue] [RequiredIfFalse] [RequiredIfEmpty] [RequiredIfNotEmpty] [RequiredIfRegExMatch] [RequiredIfNotRegExMatch] [Is] [EqualTo] [NotEqualTo] [GreaterThan] [LessThan] [GreaterThanOrEqualTo] [LessThanOrEqualTo] 

Hope this helps you!

+7
source share

There are two parts. First, you must write the required attribute, which is only required if another property meets your criteria.

You will need to do something like:

 public class RequiredComparerAttribute : RequiredAttribute { public OtherProperty { get; set; } public override bool IsValid(object value) { // TODO: use reflection to validate other property as PropertyInfo // or validate it value after it is decided to be valid foreach (ValidationAttribute va in property .GetCustomAttributes(typeof(ValidationAttribute), true) .OfType<ValidationAttribute>()) { if (!va.IsValid(value)) { return false; // not required } } return true; // required } } 

Then in Application_Start in Global.asax you will need to register a validator, which you can simply reuse the RequiredAttribute validator:

  DataAnnotationsModelValidatorProvider .RegisterAdapter(typeof(RequiredComparerAttribute), typeof(RequiredAttributeAdapter)); 

If you want to add your own validator, you will have to write your own validator. Phil Haack has an example on his blog: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx

Edit: Take a look at CompareAttribute in the .NET Reflector to see how to get the value of OtherProperty. CompareAttribute also implements IClientValidatable to provide the validation rules that are needed on the client side.

I do not think CompareAttribute will work for you because you need to check that the value is required based on the contents of another property, and not compare the equality of the two properties.

Edit2: What does the validation provider do?

He adds rules to the form and provides messages for these rules. You can see exactly how the RequiredAttributeAdapter does this by loading the MVC 3 source. To understand what it does on the client side, you can open the MVC 3 page in Google Chrome, press CTRL + SHIFT + J to open the developer tool window and enter:

 $('form:first').data().unobtrusiveValidation.options 

The rule object inside the parameters indicates how to check each element, and the message object indicates the error message that will be displayed for each verification error.

Edit3: Full Example

After answering this question, I wrote a blog post with a complete example of creating a user attribute on the client (unobtrusive check) and the server. Blog post here . This example is for the 'contains' attribute, but it's pretty easy to change to become a necessary mapping.

+4
source share

All Articles