2

I've tried to extend the RequiredAttribute to make some localizations. I wrote this:

public class LocalizedRequiredAttribute : RequiredAttribute

{
    public LocalizedRequiredAttribute(string errorMessageResourceName)
    {
        this.ErrorMessageResourceName = string.IsNullOrEmpty(errorMessageResourceName) ? "Required_ValidationError" : errorMessageResourceName;
        ErrorMessageResourceType = typeof(bop.Core.Resources.Label);
    }
}

At the client side no validation message is rendered. What is wrong? Thanks for help. Luca

User907863
  • 417
  • 1
  • 6
  • 16

1 Answers1

4

In your followup comment, you specified that clientside validation wasn't working. It looks like you asked this same question here, but for the sake of StackOverflow, I will provide the answer.

The LocalizedRequiredAttribute class must also implement IClientValidatable to get clientside validation to work:

using System.Web.Mvc;
public class LocalizedRequiredAttribute : RequiredAttribute, IClientValidatable
{
    // your previous code

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        yield return new ModelClientValidationRule
        {
            // format the error message to include the property's display name.
            ErrorMessage = FormatErrorMessage(metadata.DisplayName),

            // uses the required validation type.
            ValidationType = "required"
        };
    }
}
scott.korin
  • 2,537
  • 2
  • 23
  • 36