Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating DataAnnotations with Validator class

I'm trying to validate a class decorated with data annotation with the Validator class.

It works fine when the attributes are applied to the same class. But when I try to use a metadata class it doesn't work. Is there anything I should do with the Validator so it uses the metadata class? Here's some code..

this works:

public class Persona {     [Required(AllowEmptyStrings = false, ErrorMessage = "El nombre es obligatorio")]     public string Nombre { get; set; }      [Range(0, int.MaxValue, ErrorMessage="La edad no puede ser negativa")]     public int Edad { get; set; } } 

this doesnt work:

[MetadataType(typeof(Persona_Validation))] public class Persona {     public string Nombre { get; set; }     public int Edad { get; set; } }  public class Persona_Validation {     [Required(AllowEmptyStrings = false, ErrorMessage = "El nombre es obligatorio")]     public string Nombre { get; set; }      [Range(0, int.MaxValue, ErrorMessage = "La edad no puede ser negativa")]     public int Edad { get; set; } } 

this is how I validate the instances:

ValidationContext context = new ValidationContext(p, null, null); List<ValidationResult> results = new List<ValidationResult>();  bool valid = Validator.TryValidateObject(p, context, results, true); 

thanks.

like image 453
Pablote Avatar asked Jan 12 '10 15:01

Pablote


People also ask

Which static class from the system ComponentModel DataAnnotations namespace can be used to validate objects properties and methods?

Validator Class (System.ComponentModel.DataAnnotations) Defines a helper class that can be used to validate objects, properties, and methods when it is included in their associated ValidationAttribute attributes.

Can we do validation in MVC using data annotations?

In Asp.net MVC, we can easily apply validation to web application by using Data Annotation attribute classes to model class. Data Annotation attribute classes are present in System.

What is data Annotation validator attributes in MVC?

Data annotation attributes are attached to the properties of the model class and enforce some validation criteria. They are capable of performing validation on the server side as well as on the client side. This article discusses the basics of using these attributes in an ASP.NET MVC application.


1 Answers

I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx

MVC recognizes the MetaDataType attribute, but other projects do not. Before validating, you need to manually register the metadata class:

TypeDescriptor.AddProviderTransparent(             new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Persona), typeof(Persona_Validation)), typeof(Persona));  ValidationContext context = new ValidationContext(p, null, null); List<ValidationResult> results = new List<ValidationResult>();  bool valid = Validator.TryValidateObject(p, context, results, true); 
like image 68
Jeremy Gruenwald Avatar answered Sep 27 '22 17:09

Jeremy Gruenwald