I am currently using MVC Data Annotations to perform validation on my model.
[MinLength(4, ErrorMessage = "The {0} must be at least {2} characters long")]
[MaxLength(16, ErrorMessage = "The {0} must be {2} characters long or less")]
[DataType(DataType.Password)]
[Display(Name = "New Password")]
public string Password { get; set; }
However, I’m stuck dealing with a field that is not required, but needs to have a MinLength when there is something in the input field. Simply removing
[Required]
does not help. Is there any way to do this without creating yet another custom validation attribute?
Seems like your property has empty or white space string value, because
MinLengthattribute considersnullas a valid value: