Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had the same problem. I needed to validate complex objects that can appear as a property of another object. I haven't gotten into client side validation (yet), but the idea from Adrian Grigore about multiple html.ClientSideValidation() seems like it may be the ticket there.</p> <p>I ended up creating a marker interface that marks all the classes that I need to validate. It could be an attribute or you could use this idea for all properties of a class.</p> <p>Basically it validates the object using the DataAnnotationsValidationRunner you mention above and then iterates over the properties of the object and runs the DataAnnotationsValicationRunner over all of those until there are no more to check.</p> <p>Here's pseudo code for what I did:</p> <pre><code>IEnumarable&lt;ValidationError&gt; GetErrors(object instance) { List&lt;ValidationError&gt; errors = new List&lt;ValidationError&gt;(); errors.AddRange(GetDataAnnotationErrors(instance)); errors.AddRange(GetPropertyErrors(instance)); return errors; } IEnumerable&lt;ValidationError&gt; GetDataAnnotationErrors(object instance) { // code very similar to what you have above } IEnumearable&lt;ValidationError&gt; GetPropertyErrors(object instance) { var errors = new List&lt;ValidationError&gt;(); var objectsToValidate = instance.GetType().GetProperties().Where(p =&gt; p.PropertyType.GetInterface().Contains(typeof(IMarkerInterface))); // the call above could do any type of reflecting over the properties you want // could just check to make sure it isn't a base type so that all custom // object would be checked if(objectsToValidate == null) return errors; foreach(object obj in objectsToValidate) { errors.AddRange(GetDataAnnotationErrors(obj)); errors.AddRange(GetPropertyErrors(obj)); } return errors; } </code></pre> <p>I hope this is clear. I've been testing this system on domain objects and so far so good. Working out a few kinks here and there, but the idea has proven sound for what I'm doing. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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