3

I am trying to override the max length error message in ASP.net MVC. Basically, I would like to make the error message string as follow:

[DisplayName] length should not be more than [x]. However, I do not know how to include the displayname attribute value inside.

public class MyMaxLengthAttribute : MaxLengthAttribute
{
    public MyMaxLengthAttribute(int length) : base(length)
    {
        ErrorMessage = "What should I input here"
    }
}
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
stackdisplay
  • 1,949
  • 6
  • 29
  • 46

2 Answers2

5

If you use the StringLengthAttribute {0} refers to the display name and {2} refers to the length.

public class MyMaxLengthAttribute : StringLengthAttribute
{
    public MyMaxLengthAttribute(int length) : base(length)
    {
        ErrorMessage = "{0} length should not be more than {2}"
    }
}
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
  • Yup. this is probably what I want. May i know where to get reference on what {0}, {1}, {2} refers for each and every attribute. besides, is it possible to straight away override the stringlength attribute without inheriting? – stackdisplay Oct 28 '15 at 14:59
  • @stackdisplay I ended up just testing it with an existing one i.e. `[StringLength(100, ErrorMessage = "0: {0},1: {1}, 2: {2}, "`. I couldn't find any documentation on it. :) – hutchonoid Oct 28 '15 at 15:11
  • 1
    @stackdisplay Actually someone has detailed it here. `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."` http://stackoverflow.com/questions/13425320/what-parameters-does-the-stringlength-attribute-errormessage-take – hutchonoid Oct 28 '15 at 15:12
1

Hi I don't have access to a computer here, but I believe you have to do something along the lines of.

public class MyMaxLengthAttribute : MaxLengthAttribute
{
     private static String CustomErrorMessage = "{0} length should not be more than {1}";
     public MyMaxLengthAttribute(int length) : base(length)
     {
         ErrorMessage = "What should I input here"
     }

     public override string FormatErrorMessage(string name)
     {
        if (!String.IsNullOrEmpty(ErrorMessage))
        {
            ErrorMessage = MyErrorMessage;
        }
        return String.Format(CultureInfo.CurrentUICulture, CustomErrorMessage , name);
     }
}
Mō Iđɍɨɇƶ
  • 331
  • 1
  • 8
  • Hi, what is difference in overriding the ErrorMessage in MyMaxLengthAttribute and FormatErrorMessage? Any advantages and disadvatanges? It seems to me that both give the same outcome. – stackdisplay Oct 28 '15 at 15:01
  • Hi, to be honest I am not sure. They seem to be doing the same things but they have different definitions i.e. MSDN [ErrorMessage](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.errormessage(v=vs.110).aspx) / MSDN [FormatErrorMessage](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute.formaterrormessage(v=vs.110).aspx). – Mō Iđɍɨɇƶ Oct 28 '15 at 15:09