0

Is it possible in ASP.NET MVC for a CustomValidationAttribute on one field to execute only if a different CustomValidationAttribute validates a different field.

My view needs to contain separate date and time fields. I have separate custom validation attributes for both. But is it possible that the time validation attribute is checked only when date validation attribute validates to true ?

Thanks.

1
  • Why do you want to do that? If both the date and time are invalid, why not say so? Or why not use client side validation to do this instead, which will do what you want? Commented Mar 30, 2011 at 7:11

1 Answer 1

3

time validation attribute is checked only when date validation attribute validates to true ?

This statement means custom validation. Yes, you can do this. You Could Define custom validation attribute that takes other field's name as parameter. Then, in overrided Validate() method, you could get PropertyInfo for that other field by the name, then get validation attributes and validate them. After getting the result, you could decide whether to do validation on first field or not. Brad Wilson had great post on mvcConf about validation

By the way, you could also implement IClientValidatable to wrap up client side validation

This is the very very sample code and it needs some argument checking and error handling, etc. But i think idea is clear

 public class OtherFieldDependentCustomValidationAttribute : ValidationAttribute
{
    public readonly string _fieldName;

    public OtherFieldDependentCustomValidationAttribute(string otherFieldName)
    {
        _fieldName = otherFieldName;
    }

    protected override System.ComponentModel.DataAnnotations.ValidationResult IsValid(object value, System.ComponentModel.DataAnnotations.ValidationContext validationContext)
    {
        //Get PropertyInfo For The Other Field
        PropertyInfo otherProperty = validationContext.ObjectType.GetProperty(_fieldName);

        //Get ValidationAttribute of that property. the OtherFieldDependentCustomValidationAttribute is sample, it can be replaced by other validation attribute
        OtherFieldDependentCustomValidationAttribute attribute = (OtherFieldDependentCustomValidationAttribute)(otherProperty.GetCustomAttributes(typeof(OtherFieldDependentCustomValidationAttribute), false))[0];

        if (attribute.IsValid(otherProperty.GetValue(validationContext.ObjectInstance, null), validationContext) == ValidationResult.Success)
        {
            //Other Field Is valid, do some custom validation on current field

            //custom validation....

            throw new ValidationException("Other is valid, but this is not");
        }
        else
        {
            //Other Field Is Invalid, do not validate current one
            return ValidationResult.Success;
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

@archil : can you give some pseudocode? I am not able to follow you entirely.
have you checked up the video of Brad Wilson that i mentioned?
yes i did, that video is based on mvc 3, the project i am working on needs to use mvc 2
Phil's post was nice, but I am still not able figure out how to do it in mvc 2, can someone please help me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.