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?
I’m using System.ComponontModel.DataAnnotations to validate my model objects. How could I replace messages standard
Share
Writing new post because I need more formatting than comments provide.
Look at ValidationAttribute – base class of validation attributes.
If validation error occured, error message will be created by method:
Next look at ErrorMessageString property:
Property ResourceAccessor can be setted from:
First of it is exactly used by dervided classes to format messages, second – the case when we set error message trough ErrorMessage property, and third – when resource strings used.
Depending on your situation, you may use ErrorMessageResourceName.
Elsewhere let’s look at derived constructors, for our example, Range Attribute:
Here RangeAttribute_ValidationError is loaded from resource:
So you can create resource file for different tan default culture and overwrite messages there, like this:
http://www.codeproject.com/KB/aspnet/SatelliteAssemblies.aspx
http://msdn.microsoft.com/en-us/library/aa645513(VS.71).aspx