Tyler Smith
Tyler Smith

Reputation: 1279

Compound Validation

I have a dropdown, where if a certain item is selected a subform pops up (hidden div that appears) with more stuff that needs to be filled out. What is the best approach to validation for something like this? I thought about writing a validator (with ValidationAttribute, IClientValidatable) but this means i have to take that group of boxes out from the rest of the form so they can be validated as 1 object.

Thanks in advance.

Follow Up: I found this that does what I want client-side! http://foolproof.codeplex.com/
[RequiredIf]
[RequiredIfNot]
[RequiredIfTrue]
[RequiredIfFalse]
[RequiredIfEmpty]
[RequiredIfNotEmpty]
[RequiredIfRegExMatch]
[RequiredIfNotRegExMatch]

_
Nevermind already found a bunch of problems with foolproof

Upvotes: 0

Views: 314

Answers (1)

Chris Knight
Chris Knight

Reputation: 1476

You will need to extend the ValidationAttribute to create a custom validator. It could be something like this:

        public YourValidatorNameAttribute()
        {
            ErrorMessage =  /** your not valid messaging **/;
        }  



        public override bool IsValid(object value)
        {
            bool isValid = true;
            YourClass c = value as YourClass;
            if (c != null)
            {
                if (/** check if the item in your dropdown is selected**/)
                {
                    isValid = /** check value of fields or whatever validation is needed in the 'more stuff' fields**/;
                }
            }
            return isValid;
        }

Upvotes: 1

Related Questions