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.
BrockAllen
All-Star
27530 Points
4905 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/