Note that there are some explanatory texts on larger screens.

plurals
  1. POxVal How do I validate child properties of complex types?
    text
    copied!<p>I'm using <a href="http://www.codeplex.com/xval" rel="nofollow noreferrer">xVal</a> in my ASP.NET MVC application, which is great in general. Following <a href="http://blog.codeville.net/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/" rel="nofollow noreferrer">Steve Sanderson's blog post</a>, I created a DataAnnotationsValidationRunner to do server-side validation of attributed objects. This works great for a simple class. e.g. Person:</p> <pre><code>public static class DataAnnotationsValidationRunner { public static IEnumerable&lt;ErrorInfo&gt; GetErrors(object o) { return from prop in TypeDescriptor.GetProperties(o).Cast&lt;PropertyDescriptor&gt;() from attribute in prop.Attributes.OfType&lt;ValidationAttribute&gt;() where !attribute.IsValid(prop.GetValue(o)) select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), o); } } public class Person { [Required(ErrorMessage="Please enter your first name")] public string FirstName { get; set; } [Required(ErrorMessage = "Please enter your last name")] public string LastName { get; set; } } </code></pre> <p>However, if I add an Address property to this person, and mark the Address class with DataAnnotation attributes, they will not be validated. e.g. </p> <pre><code>public class Person { [Required(ErrorMessage="Please enter your first name")] public string FirstName { get; set; } [Required(ErrorMessage = "Please enter your last name")] public string LastName { get; set; } public Address Address { get; set; } } public class Address { [Required(ErrorMessage="Please enter a street address")] public string Street { get; set; } public string StreetLine2 { get; set; } [Required(ErrorMessage = "Please enter your city")] public string City { get; set; } [Required(ErrorMessage = "Please enter your state")] public string State { get; set; } [Required(ErrorMessage = "Please enter your zip code")] public string Zip { get; set; } public string Country { get; set; } } </code></pre> <p>One problem is that the DataAnnotationValidationRunner doesn't walk down the complex child properties. Also, if those errors are added to an errors collection, they still need to get prefixed correctly when added to the model state. For example. The Person errors are added like this:</p> <pre><code> catch (RulesException ex) { ex.AddModelStateErrors(ModelState, "person"); } </code></pre> <p>I think the Address rules exceptions would need to be prefixed with "person.address". Is there a supported way of handling child object validation with xVal, or would creating a flattened data transfer object be the only solution?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload