Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace standard DataAnnotations error messages

I'm using System.ComponontModel.DataAnnotations to validate my model objects. How could I replace messages standard attributes (Required and StringLength) produce without providing ErrorMessage attribute to each of them or sub classing them?

like image 978
Artem Tikhomirov Avatar asked Feb 01 '10 12:02

Artem Tikhomirov


People also ask

How to validate ModelState in MVC?

When MVC creates the model state for the submitted properties, it also iterates through each property in the view model and validates the property using attributes associated to it. If any errors are found, they are added to the Errors collection in the property's ModelState . Also note that ModelState.

What are validation annotations?

Data Annotations are nothing but certain validations that we put in our models to validate the input from the user. ASP.NET MVC provides a unique feature in which we can validate the models using the Data Annotation attribute. Import the following namespace to use data annotations in the application.

What is ModelState in ASP net Core?

Model state represents errors that come from two subsystems: model binding and model validation. Errors that originate from model binding are generally data conversion errors. For example, an "x" is entered in an integer field.


1 Answers

You can use ErrorMessage property of base class ValidationAttribute for all DataAnnotations validators.

For example:

[Range(0, 100, ErrorMessage = "Value for {0} must be between {1} and {2}")]
public int id;

Maybe it'll help.

like image 67
Vitaliy Ulantikov Avatar answered Nov 24 '22 23:11

Vitaliy Ulantikov