Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I also ran into this, and found this thread. Here's a first pass:</p> <pre><code>namespace Foo { using System.ComponentModel.DataAnnotations; using System.Linq; /// &lt;summary&gt; /// Attribute class used to validate child properties. /// &lt;/summary&gt; /// &lt;remarks&gt; /// See: http://stackoverflow.com/questions/2493800/how-can-i-tell-the-data-annotations-validator-to-also-validate-complex-child-pro /// Apparently the Data Annotations validator does not validate complex child properties. /// To do so, slap this attribute on a your property (probably a nested view model) /// whose type has validation attributes on its properties. /// This will validate until a nested &lt;see cref="System.ComponentModel.DataAnnotations.ValidationAttribute" /&gt; /// fails. The failed validation result will be returned. In other words, it will fail one at a time. /// &lt;/remarks&gt; public class HasNestedValidationAttribute : ValidationAttribute { /// &lt;summary&gt; /// Validates the specified value with respect to the current validation attribute. /// &lt;/summary&gt; /// &lt;param name="value"&gt;The value to validate.&lt;/param&gt; /// &lt;param name="validationContext"&gt;The context information about the validation operation.&lt;/param&gt; /// &lt;returns&gt; /// An instance of the &lt;see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/&gt; class. /// &lt;/returns&gt; protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var isValid = true; var result = ValidationResult.Success; var nestedValidationProperties = value.GetType().GetProperties() .Where(p =&gt; IsDefined(p, typeof(ValidationAttribute))) .OrderBy(p =&gt; p.Name);//Not the best order, but at least known and repeatable. foreach (var property in nestedValidationProperties) { var validators = GetCustomAttributes(property, typeof(ValidationAttribute)) as ValidationAttribute[]; if (validators == null || validators.Length == 0) continue; foreach (var validator in validators) { var propertyValue = property.GetValue(value, null); result = validator.GetValidationResult(propertyValue, new ValidationContext(value, null, null)); if (result == ValidationResult.Success) continue; isValid = false; break; } if (!isValid) { break; } } return result; } } } </code></pre>
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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