{0} must be at least {2} characters long

I have a Visual Studio 2013 MVC Razor project that I am exploring by going through one of the examples on w3schools.com .

In the ASP.NET MVC Security chapter, you will see a default file AccountModels.cs

in the Models class with the following text for each of the Password fields :

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New password")]
    public string NewPassword { get; set; }

      

I am familiar with String.Format where parameters must start at 0 and increment.

The second parameter above, however, jumps to 2, and it doesn't have enough parameters passed to the string.

When exploring a project, I do everything in my power to tweak features (such as string responses) to better support my learning.

What's going on here?

pictures rock

+3


source to share


2 answers


StringLengthAttribute class constructor

We basically call the constructor of the class:

StringLengthAttribute

is a class and its constructor has one parameter int

calledmaximumLength

It also has several Properties

:

  • ErrorMessage

    type string

    • in our case, this is the line: "The {0} must be at least {2} characters long."

  • ErrorMessageResourceName

    type string

    • in our case, we do not attach importance to it
  • ErrorMessageResourceType

    type System.Type

    • in our case, we do not attach importance to it
  • MinimumLength

    type int

    • in our case we will give it a value 6

the string ErrorMessageResourceName

has nothing to do with other properties, so it doesn't look like this:

String.Format("some variable {0} and some other {1}...", 100, 6)

blow>

so number 100

and property MinimumLength = 6

not all (yet) parameters are sent for formatting with a string "The {0} must be at least {2} characters long."

.



The class StringLengthAttribute

also has some methods, one of which is calledFormatErrorMessage

This method is called internally to format the message, and it internally formats the string using String.Format

, and that's when the parameters are passed to the string to be formatted correctly.

  • {0} is bound to DisplayName
  • {1} is bound to MaximumLength
  • {2} bound to MinimumLength

this is the method called internal (if you want to know how it does it internally):

/// <summary>
    /// Override of <see cref="ValidationAttribute.FormatErrorMessage"/>
    /// </summary>
    /// <param name="name">The name to include in the formatted string</param>
    /// <returns>A localized string to describe the maximum acceptable length</returns>
    /// <exception cref="InvalidOperationException"> is thrown if the current attribute is ill-formed.</exception>
    public override string FormatErrorMessage(string name) {
        this.EnsureLegalLengths();

        bool useErrorMessageWithMinimum = this.MinimumLength != 0 && !this.CustomErrorMessageSet;

        string errorMessage = useErrorMessageWithMinimum ?
            DataAnnotationsResources.StringLengthAttribute_ValidationErrorIncludingMinimum : this.ErrorMessageString;

        // it 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);
    }

      

Literature:

  • Microsoft reference source here
  • A similar question on Stackoverflow: "What parameters does the errlessessage of the stringlength attribute take?" here
  • Almost useless msdn documentation for StringLengthAttribute Class

    here
+3


source


After doing a little research, I found the answer on the ASP.NET Forum from CodeHobo :

You can find complete documentation here

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.stringlengthattribute.aspx

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

In this case, the error message is a string pattern that is applied during rendering. Think about a string. So it is equivalent to

string.Format ("{0} must contain at least {2} characters.", DisplayName, MaximumLength, MinimumLength);

Index 0 is the display name of the property, 1 is the maximum length, 2 is the minimum length

In your example, this displays the display name instead of the minimum length. You need to change {0} to {2}

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

Yes, I could just delete my question, but SO is my main source of programming information.



If I don't find a programming answer here, I feel like it needs it.

I don't quite understand the answer 100%, so if anyone has a better answer I would happily accept it.

+3


source







All Articles