39

In the MVC4 template one of the data annotation attributes used is stringlength.

For example:

[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]

What parameters {0}, {1}, {2} (more?) are legal?

Edit: To be more specific, I can see from the example and trial and error what the possibilities are, but I would like to see some hard documentation.

I can't find anything about this in the StringLengthAttribute documentation.

Anders E. Andersen
  • 1,635
  • 2
  • 14
  • 20

3 Answers3

54

The {0} index is the display name of property, {1} is the MaximumLength, {2} is the MinimumLength. So, your error message will be formate as "The Foo must be at least 6 characters long."

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 22
    Yes, well I guess what I am really asking is, how can I know this, apart from trial and error. Is it implemented in the StringLengthAttribute class itself in the [FormatErrorMessage](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.formaterrormessage%28v=vs.110%29.aspx) method? Is this documented? – Anders E. Andersen Nov 16 '12 at 22:33
  • 2
    What if `MinimumLength` wasn't specified? And if what if it's the only property that was set while `MaximumLength` is zero? – Shimmy Weitzhandler Nov 30 '15 at 03:22
  • No idea if it was true many years ago when this was answered, but the docs currently state this in remarks. https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.stringlengthattribute?view=netcore-3.1#remarks – AjimOthy Jul 21 '20 at 18:37
12

I haven't seen any documentation either, but the FormatErrorMessage method for the StringLengthAttribute looks like this:

public override string FormatErrorMessage(string name)
{
    EnsureLegalLengths();
    string format = ((this.MinimumLength != 0) && !base.CustomErrorMessageSet) ? DataAnnotationsResources.StringLengthAttribute_ValidationErrorIncludingMinimum : base.ErrorMessageString;
    return String.Format(CultureInfo.CurrentCulture, format, new object[] { name, MaximumLength, MinimumLength });
}
Oundless
  • 5,425
  • 4
  • 31
  • 33
7

A longer-than-expected Google search brought me to this old topic before I could start getting some solid leads, so I'll put this here and hope it helps anyone else in the same shoes:

Inspecting the code for StringLengthAttribute that MS put up on GitHub confirms the logic residing in the FormatErrorMessage method:

// it's ok to pass in the minLength even for the error message without a {2} param since String.Format will just
// ignore extra arguments
return String.Format(CultureInfo.CurrentCulture, errorMessage, name, this.MaximumLength, this.MinimumLength);

Thus '0', '1', and '2' corresponds to 'name' (of Property), 'MaximumLength', and 'MinimumLength' accordingly.

I bet the same method can be applied to all other validation attributes to check their formatting parameters accordingly; I was not able to find any other documentation for this infomation otherwise.

Bo Ngoh
  • 133
  • 1
  • 8