Thilok Gunawardena
Thilok Gunawardena

Reputation: 924

Enterprise Library Validation Application Block. How to validate the model class?

I am using Enterprise Library 5.0 - Validation Application Block 5.0.505.0 in my project. I have modified my Model class attributes with the validation annotations (like NotNullValidator). But when I run my project and fill the particular Model class with data, it does not automatically validate the Model. Do I have to test the Model manually as below?

USAddress testaddress = new USAddress(); //this is the Model instance which I am validating

        //Create a new validator using the ValidationFactory method
        Validator validator = ValidationFactory.CreateValidator<USAddress>();
        ValidationResults results = new ValidationResults();
        validator.Validate(testaddress, results);

Do I have to validate the Model class like this everytime? Doesn't it get automatically validated when I fill the data to the attributes of this class?

Upvotes: 1

Views: 218

Answers (1)

Sean
Sean

Reputation: 2577

Validation with models should use:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

You need to make references to:

System.ComponentModel.DataAnnotations

You will then have access to attributes like:

[Required]
[StringLength]
[RegularExpression]
[Compare]

[Required] is similar to the [NotNullValidator].

HTH!

Upvotes: 1

Related Questions