I have such attribute which is checking for minimum and maximum string length:
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
public string PropertyName {get; set;}
Basically it is how it is declared in default asp.net mvc 3.0 template(they just have Maximum 100).
So if string contain less than 3 characters the display error message will be
"The PropertyName must be at least 3 characters long."
So I don't see any problems wit that in such case
But if string length will be more than 10 the display message still will be
"The PropertyName must be at least 3 characters long."
So it is getting not correct now
But if i change the message template to something like that:
[StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long and maximum {1} characters long", MinimumLength = 3)]
The error message for both cases will be like that:
"The PropertyName must be at least 3 characters long and maximum 10 characters long"
And it is not so good as well
1) The message is to long
2) It would be better to display separate message for each case so and question is how could I do hat?
Is there any way I could display two different messages, one message if length is less than minimum and another message is length is greater than maximum?
I could see only one way to fix that is to declare two separate StringLength
attributes one would check for minimum an another for maximum but it is impossible to add two same attributes on method, also I would go with MinLength
and MaxLength
attributes but they not generating unobtrusive attributes. So looks like I should write my custom validation attribute
But it would be great to display two different messages on each case (min and max) for one StringLength
validation attribute if it would be possible, what do you think?
You can use MinLength, MaxLength attributes. It is clearer than two StringLength attributes
[MinLength(3, ErrorMessage="The {0} must be at least {2} characters long")]
[MaxLength(10, ErrorMessage="The {0} must be maximum {2} characters long")]
public string PropertyName { get; set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With