Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I solved it. It would appear that the <a href="http://www.codeproject.com/KB/validation/Data_Annotations_with_ASP.aspx?msg=3603632&amp;display=Mobile" rel="nofollow">server control I found </a> was not designed to read fields in a buddy class via the MetadataType attribute. I modified the code to look for its validation attributes in the buddy class rather than the entity class itself.</p> <p>Here is the modified version of the linked server control:</p> <pre><code> [DefaultProperty("Text")] [ToolboxData("&lt;{0}:DataAnnotationValidator runat=server&gt;&lt;/{0}:DataAnnotationValidator&gt;")] public class DataAnnotationValidator : BaseValidator { #region Properties /// &lt;summary&gt; /// The type of the source to check /// &lt;/summary&gt; public string SourceTypeName { get; set; } /// &lt;summary&gt; /// The property that is annotated /// &lt;/summary&gt; public string PropertyName { get; set; } #endregion #region Methods protected override bool EvaluateIsValid() { // get the type that we are going to validate Type source = GetValidatedType(); // get the property to validate FieldInfo property = GetValidatedProperty(source); // get the control validation value string value = GetControlValidationValue(ControlToValidate); foreach (var attribute in property.GetCustomAttributes( typeof(ValidationAttribute), true) .OfType&lt;ValidationAttribute&gt;()) { if (!attribute.IsValid(value)) { ErrorMessage = attribute.ErrorMessage; return false; } } return true; } private Type GetValidatedType() { if (string.IsNullOrEmpty(SourceTypeName)) { throw new InvalidOperationException( "Null SourceTypeName can't be validated"); } Type validatedType = Type.GetType(SourceTypeName); if (validatedType == null) { throw new InvalidOperationException( string.Format("{0}:{1}", "Invalid SourceTypeName", SourceTypeName)); } IEnumerable&lt;MetadataTypeAttribute&gt; mt = validatedType.GetCustomAttributes(typeof(MetadataTypeAttribute), false).OfType&lt;MetadataTypeAttribute&gt;(); if (mt.Count() &gt; 0) { validatedType = mt.First().MetadataClassType; } return validatedType; } private FieldInfo GetValidatedProperty(Type source) { FieldInfo field = source.GetField(PropertyName); if (field == null) { throw new InvalidOperationException( string.Format("{0}:{1}", "Validated Property Does Not Exists", PropertyName)); } return field; } #endregion } </code></pre> <p>This code <em>only</em> looks in the buddy class. If you want it to check an actual class and then its buddy class, you'll have to modify it accordingly. I did not bother doing that because usually if you are using a buddy class for validation attributes it's because you are not able to use the attributes in the main entity class (e.g. Entity Framework).</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