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.
you can iether pass the name of the other field in the validation attribute constructor, or have the model implement an interface to get the other field. it depends on how generic you want the validator.
One approach is to create a custom validation attribute and apply that to the field on your model that you're trying to validate. So for example, some built in validators might be applied like this to a model:
class Person
{
[Range(0, 10, ErrorMessage="You must be between 0 and 10 years old.")]
public int Age {get; set;}
}
So you can write your own validation attribute like the one above. Just derive from
ValidationAttribute and override IsValid.
you can iether pass the name of the other field in the validation attribute constructor, or have the model implement an interface to get the other field. it depends on how generic you want the validator.
I do have a custom validation attribute that works quite well. Presently it accepts an object:
One approach is to create a custom validation attribute and apply that to the field on your model that you're trying to validate. So for example, some built in validators might be applied like this to a model:
class Person
{
[Range(0, 10, ErrorMessage="You must be between 0 and 10 years old.")]
public int Age {get; set;}
}
So you can write your own validation attribute like the one above. Just derive from
ValidationAttribute and override IsValid.
Ok... but what about when the values you're passing in come from another field?
I tried this:
public Nullable<int> SchoolYear { get; set; }
[StudentBirthdateValidation(SchoolYear.Value, ErrorMessage = "Test")]
public Nullable<System.DateTime> S1BirthDay { get; set; }
I'm getting an error that I can't access a non-static value in a statuc context though. :\
Ah so you want cross-field validation? Then there's another IsValid to override where the conatiner object is passed. You can either downcast or use reflection to access the other properties.
BTW, the other approacj is to have the model implement IValidatableObject -- this is a little more direct/easier than doing cross field validation with validation attributes.
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.
bruce (sqlwo...
All-Star
36822 Points
5441 Posts
Re: What's the Best Way to Use a Value from Form Field within Custom Validator
Mar 27, 2012 01:29 PM|LINK
you can iether pass the name of the other field in the validation attribute constructor, or have the model implement an interface to get the other field. it depends on how generic you want the validator.
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: What's the Best Way to Use a Value from Form Field within Custom Validator
Mar 27, 2012 01:30 PM|LINK
One approach is to create a custom validation attribute and apply that to the field on your model that you're trying to validate. So for example, some built in validators might be applied like this to a model:
class Person
{
[Range(0, 10, ErrorMessage="You must be between 0 and 10 years old.")]
public int Age {get; set;}
}
So you can write your own validation attribute like the one above. Just derive from ValidationAttribute and override IsValid.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Bleak Morn
Member
29 Points
60 Posts
Re: What's the Best Way to Use a Value from Form Field within Custom Validator
Mar 27, 2012 02:15 PM|LINK
I do have a custom validation attribute that works quite well. Presently it accepts an object:
public override bool IsValid(object value) { DateTime studentBirthDate = Convert.ToDateTime(value); if (!(studentBirthDate == DateTime.MinValue) && !BirthDatesAreValid(studentBirthDate)) return false; return true; }I'd like to pass the value as part of that object - but here is how my custom validator is called:
How does one pass the value into a custom validation attribute?
UPDATE: I think BrockAllen might have answered my question.
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: What's the Best Way to Use a Value from Form Field within Custom Validator
Mar 27, 2012 02:19 PM|LINK
Which value are you talking about? You get as a paramater the birthday... so what other value are you looking for?
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
Bleak Morn
Member
29 Points
60 Posts
Re: What's the Best Way to Use a Value from Form Field within Custom Validator
Mar 27, 2012 02:36 PM|LINK
Ok... but what about when the values you're passing in come from another field?
I tried this:
public Nullable<int> SchoolYear { get; set; } [StudentBirthdateValidation(SchoolYear.Value, ErrorMessage = "Test")] public Nullable<System.DateTime> S1BirthDay { get; set; }I'm getting an error that I can't access a non-static value in a statuc context though. :\
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: What's the Best Way to Use a Value from Form Field within Custom Validator
Mar 27, 2012 02:39 PM|LINK
Ah so you want cross-field validation? Then there's another IsValid to override where the conatiner object is passed. You can either downcast or use reflection to access the other properties.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
BrockAllen
All-Star
27512 Points
4895 Posts
MVP
Re: What's the Best Way to Use a Value from Form Field within Custom Validator
Mar 27, 2012 02:45 PM|LINK
BTW, the other approacj is to have the model implement IValidatableObject -- this is a little more direct/easier than doing cross field validation with validation attributes.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/