Search code examples
asp.net-mvc-3unobtrusive-validationvalidationattribute

Accessing ErrorMessage from within custom validation attribute


I want to be able to read the ErrorMessage string passed into a custom validation attribute so I can do some string.Format()ing on it. But I can't seem to find it?

So for example I have a custom attribute:

[IsDateBeforeFixedDate(4, 0, 0, ErrorMessage = "*The departure date should be between 4 days and 11 months")]

decalred as:

  public sealed class IsDateBeforeFixedDateAttribute : ValidationAttribute, IClientValidatable
  {

    public IsDateBeforeFixedDateAttribute(int days, int months, int years) : base(days, months, years)
    {
      //I want to read the ErrorMessage string here! (i.e. "*The departure date should be between 4 days and 11 months")
    }
  }

ErrorMessageString doesn't contain it or ErrorMessage


Solution

  • Appears that if I access it in the IsValid method it contains the right string but it doesn't in the contructor