Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the DataAnnotations.Validator class, as described here:</p> <p><a href="http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx" rel="noreferrer">http://johan.driessen.se/archive/2009/11/18/testing-dataannotation-based-validation-in-asp.net-mvc.aspx</a></p> <p>But if you're using a "buddy" class for the metadata, you need to register that fact before you validate, as described here:</p> <p><a href="http://forums.silverlight.net/forums/p/149264/377212.aspx" rel="noreferrer">http://forums.silverlight.net/forums/p/149264/377212.aspx</a></p> <pre><code>TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(myEntity), typeof(myEntityMetadataClass)), typeof(myEntity)); List&lt;ValidationResult&gt; results = new List&lt;ValidationResult&gt;(); ValidationContext context = new ValidationContext(myEntity, null, null) bool valid = Validator.TryValidateObject(myEntity, context, results, true); </code></pre> <p><strong>[Added the following to respond to Shimmy's comment]</strong></p> <p>I wrote a generic method to implement the logic above, so that any object can call it:</p> <pre><code>// If the class to be validated does not have a separate metadata class, pass // the same type for both typeparams. public static bool IsValid&lt;T, U&gt;(this T obj, ref Dictionary&lt;string, string&gt; errors) { //If metadata class type has been passed in that's different from the class to be validated, register the association if (typeof(T) != typeof(U)) { TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(T), typeof(U)), typeof(T)); } var validationContext = new ValidationContext(obj, null, null); var validationResults = new List&lt;ValidationResult&gt;(); Validator.TryValidateObject(obj, validationContext, validationResults, true); if (validationResults.Count &gt; 0 &amp;&amp; errors == null) errors = new Dictionary&lt;string, string&gt;(validationResults.Count); foreach (var validationResult in validationResults) { errors.Add(validationResult.MemberNames.First(), validationResult.ErrorMessage); } if (validationResults.Count &gt; 0) return false; else return true; } </code></pre> <p>In each object that needs to be validated, I add a call to this method:</p> <pre><code>[MetadataType(typeof(Employee.Metadata))] public partial class Employee { private sealed class Metadata { [DisplayName("Email")] [Email(ErrorMessage = "Please enter a valid email address.")] public string EmailAddress { get; set; } } public bool IsValid(ref Dictionary&lt;string, string&gt; errors) { return this.IsValid&lt;Employee, Metadata&gt;(ref errors); //If the Employee class didn't have a buddy class, //I'd just pass Employee twice: //return this.IsValid&lt;Employee, Employee&gt;(ref errors); } } </code></pre>
    singulars
    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. 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.
 

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