I am trying to create a minimum length check attribute that will force users to enter a given minimum number of characters in a text box
public sealed class MinimumLengthAttribute : ValidationAttribute { public int MinLength { get; set; } public MinimumLengthAttribute(int minLength) { MinLength = minLength; } public override bool IsValid(object value) { if (value == null) { return true; } string valueAsString = value as string; return (valueAsString != null && valueAsString.Length >= MinLength); } }
In the MinimumLengthAttribute constructor, I would like to set the error message as follows:
ErrorMessage = "{0} must be at least {1} characters"
How can I get the display name of a property to populate the {0} placeholder?
source share