Validation context does not validate validation attributes

I got confused with a strange scenario and I hope it will be a chair and keyboard bug.

I cannot get the validation context to validate any other validation attribute.

here is my poco:

  public class TestMe
  {
    [System.ComponentModel.DataAnnotations.Range(1,40)]
    public int Count { get; set; }

  }

      

and i run

  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. he only checks RequiredAttribute

. I tried to create a new attribute to check if IsValid was called and to my frustration none of the IsValid functions were executed. I tried RequiredAttribute

it and that one seems to be getting a challenge.

Does anyone know what I am doing wrong?

+3


source to share


1 answer


try it

var result = Validator.TryValidateObject(t, context, validationResults, true);

      



You must use a parameter validateAllProperties

in TryValidateObject

. Install it on true

. that it should use the range validator as it should.

hope this helped!

+3


source







All Articles