When you decorate a model object's property with the Required
attribute and don't specify ErrorMessage
or ResourceType/Name
you get the validation message in the interpolated form of "The {0} field is required.", where param 0 is the value of the DisplayName
attribute of that property.
I want to change that default string to something else but I want to keep the generic nature of it, that is I don't want to specify ErrorMessage
or ResourceType/Name
for every property of the model object. Where is the default string stored and how can I change it?
Adding Validation to the Model (C#) 1 Keeping Things DRY. One of the core design tenets of ASP.NET MVC is DRY ("Don't Repeat Yourself"). ... 2 Validation Error UI in ASP.NET MVC. Re-run the application and navigate to the /Movies URL. Click the Create Movie link to add a new movie. 3 Adding Formatting to the Movie Model. Open the Movie.cs file. ...
The Html.ValidationMessageFor () is a strongly typed extension method. It displays a validation message if an error exists for the specified field in the ModelStateDictionary object. The ValidationMessageFor () method will only display an error if you have configured DataAnnotations attribute to the specified property in the model class.
ValidationSummary. The ValidationSummary helper method generates an unordered list (ul element) of validation messages that are in the ModelStateDictionary object. The ValidationSummary can be used to display all the error messages for all the fields. It can also be used to display custom error messages.
Right-click the Index method in HomeController. The "Add View" window will appear with default index name checked (use a Layout page). Click on "Add". The Html.ValidationMessage () is an extension method, that is a loosely typed method. It displays a validation message if an error exists for the specified field in the ModelStateDictionary object.
Deriving your own attribute is a fair option and probably has the lowest overhead to get started, but you'll need to go back and change all your existing uses of [Required]
. You (and any others on your team) will also need to remember to use (and teach newcomers to use) the right one going forward.
An alternative is to replace the ModelMetadataProviders
and ModelValidatorProviders
to return strings from a resource file. This avoids the drawbacks above. It also lays the groundwork for replacing messages for other attributes (e.g., MaxLengthAttribute
) and for supporting additional languages.
protected void Application_Start()
{
var stringProvider = new ResourceStringProvider(Resources.LocalizedStrings.ResourceManager);
ModelMetadataProviders.Current = new LocalizedModelMetadataProvider(stringProvider);
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new LocalizedModelValidatorProvider(stringProvider));
}
Here is the full source, documentation, and a blog post describing the usage.
Have you tried creating a derived class of RequiredAttribute and overriding the FormatErrorMessage method? This should work:
public class MyRequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
public override string FormatErrorMessage(string name)
{
return base.FormatErrorMessage(string.Format("This is my error for {0}", name));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With