Reputation: 1833
I'm running into a weird scenario and I'm hoping it's a chair and keyboard kind of error.
I can't seem to get the validation context to check any other validation attribute.
here is my poco:
public class TestMe
{
[System.ComponentModel.DataAnnotations.Range(1,40)]
public int Count { get; set; }
}
and I am running
var t = new TestMe();
t.Count = 0;
var context = new ValidationContext(t, null, null);
var validationResults = new List<ValidationResult>();
var result = Validator.TryValidateObject(t, context, validationResults);
this seems to return true with no errors.
it only seems to check the RequiredAttribute
. I tried creating a new attribute to check to see if IsValid is getting called and to my disappointment neither of the IsValid functions were executed. I overrode the RequiredAttribute
and that one DOES seem to be getting called.
Does anyone know what the heck I am doing wrong?
Upvotes: 3
Views: 1475
Reputation: 421
Make sure to allow the Validator.TryValidateObject() to run. If you are debugging and have a breakpoint on/before the Validator you will not see results.
Upvotes: 0
Reputation: 2917
Try this
var result = Validator.TryValidateObject(t, context, validationResults, true);
You must make use of the validateAllProperties
parameter in TryValidateObject
. Set it to true
. that's it then range validator should work as expected.
hope this helped!
Upvotes: 3