I have a boolean flag called IsFieldA, and two string fields FieldA and FieldB.

I would like to do validation on FieldA or FieldB depending on the IsFieldA value. (If IsFieldA is true, then check FieldA only. Otherwise, check FieldB only.)

I tried doing this by removing the generated validation for those two fields, and created a FieldAOrFieldB field that sets / gets the field values depending on the IsFieldA value, and check from there. The form field is bound against this hybrid field.

When I flip the IsFieldA value, I am getting validation failure because it is checking the wrong fields. It is updating the field first and then flipping the IsFieldA value after.

Is there a way to set the order of when the fields get updated? I'd like to update the switch first, and then FieldA or FieldB. Is the update order set by the order it appears in the entity?

share|improve this question

feedback

1 Answer

up vote 0 down vote accepted

You could create a custom validation attribute. By putting the attribute on the class you get access to all the fields so you can check their values and validate accordingly. This question has a good example and a link to an article by brad wilson.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.