I've written a custom validator that takes a person's birthdate and does some calculations on it to ensure that they are within the acceptable age range for some services we offer.
Presently it determines the current year and uses that as the basis for calculations.
We've added a drop-down to the form to allow users to select the following year and I need to update the custom validator to accomodate that.
It'd seem that I have two obvious options - one way is to somehow pass the value from the other field into the custom validator. The other way is to somehow retrieve it from the form data (stored in the model at validation time?) then use that to drive the
custom validator's logic.
I've looked at a number of examples and what I'm trying to do is a little more advanced. I figured I'd put this out here to get some input on the best ways to accomplish this sort of thing as I continue my research. It seems from past experience that it's
a mistake to try to fight the way MVC is designed to do things - I'm just not sure yet what approach "goes wit the MVC flow" best in this case.
Bleak Morn
Member
29 Points
60 Posts
What's the Best Way to Use a Value from Form Field within Custom Validator
Mar 27, 2012 01:21 PM|LINK
I've written a custom validator that takes a person's birthdate and does some calculations on it to ensure that they are within the acceptable age range for some services we offer.
Presently it determines the current year and uses that as the basis for calculations.
We've added a drop-down to the form to allow users to select the following year and I need to update the custom validator to accomodate that.
It'd seem that I have two obvious options - one way is to somehow pass the value from the other field into the custom validator. The other way is to somehow retrieve it from the form data (stored in the model at validation time?) then use that to drive the custom validator's logic.
I've looked at a number of examples and what I'm trying to do is a little more advanced. I figured I'd put this out here to get some input on the best ways to accomplish this sort of thing as I continue my research. It seems from past experience that it's a mistake to try to fight the way MVC is designed to do things - I'm just not sure yet what approach "goes wit the MVC flow" best in this case.