How to call ValidationAttributes manually? (DataAnnotations and ModelState)
Asked Answered
A

2

7

We have a need within some of our logic to iterate through the properties of a model to auto-bind properties and want to extend the functionality to include the new dataannotations in C# 4.0.

At the moment, I basically iterate over each property loading in all ValidationAttribute instances and attempting to validate using the Validate/IsValid function, but this doesn't seem to be working for me.

As an example I have a model such as:

public class HobbyModel
{
    [Required(AllowEmptyStrings = false, ErrorMessage = "Do not allow empty strings")]
    [DisplayName("Hobby")]
    [DataType(DataType.Text)]
    public string Hobby
    {
        get;
        set;
    }
}

And the code to check the attributes is:

object[] attributes = propertyInfo.GetCustomAttributes(true);
TypeConverter typeConverter =
TypeDescriptor.GetConverter(typeof(ValidationAttribute));

bool isValid = false;
foreach (object attr in attributes)
{
   ValidationAttribute attrib = attr as ValidationAttribute;

   if (attrib != null)
   {
     attrib.Validate(obj, propertyInfo.Name);
   }
}

I've debugged the code and the model does have 3 attributes, 2 of which are derived from ValidationAttribute, but when the code passes through the Validate function (with a empty or null value) it does thrown an exception as expected.

I'm expecting I'm doing something silly, so am wondering whether anyone has used this functionality and could help.

Thanks in advance, Jamie

Agamemnon answered 13/12, 2010 at 8:5 Comment(0)
M
4

This is because you are passing the source object to the Validate method, instead of the property value. The following is more likely to work as expected (though obviously not for indexed properties):

attrib.Validate(propertyInfo.GetValue(obj, null), propertyInfo.Name);

You would certainly have an easier time using the Validator class as Steven suggested, though.

Moonfaced answered 27/4, 2012 at 16:39 Comment(0)
P
4

You do use the System.ComponentModel.DataAnnotations.Validator class to validate objects.

Parts answered 13/12, 2010 at 8:11 Comment(3)
Have you got an example of using DataAnnotations 4.0 with ASP.Net MVC 3.0, I'm currently using 3.5 (which may or may not work, I'm upgrading code from MVC 1.0 to 3)Agamemnon
If I'm not mistaken, MVC 2 and up work with DataAnnotations by default, so when working with MVC 3 you wont have to call the validator manually.Parts
Basically we want to offload the process into a separate mechanism, so want to be able to autobind and run validation manually.Agamemnon
M
4

This is because you are passing the source object to the Validate method, instead of the property value. The following is more likely to work as expected (though obviously not for indexed properties):

attrib.Validate(propertyInfo.GetValue(obj, null), propertyInfo.Name);

You would certainly have an easier time using the Validator class as Steven suggested, though.

Moonfaced answered 27/4, 2012 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.