-
-
Notifications
You must be signed in to change notification settings - Fork 8
DataAnnotations Localization Using Default Attributes
DataAnnotations localization setup is already done during the express setup in startup.cs
file for razor pages or MVC.
All we have to do is just provide an error message to the relevant attribute or a Name
value for Display
attribute:
[Required(ErrorMessage = "The {0} field is required.")]
[Display(Name = "Full name")]
public string Name { get; set; }
For easy implementation, all default framework error messages of data annotations, model binding and identity errors are predefined under LazZiya.ExpressLocalization.Messages
namespace.
So we can use the already defined messages as below:
using LazZiya.ExpressLocalization.Messages
[Required(ErrorMessage = DataAnnotationsErrorMessages.RequiredAttribute_ValidationError)]
[Display(Name = "Full name")]
public string Name { get; set; }
See all DataAnnotationsErrorMessages
You are still able to provide custom validation messages as below:
[Required(ErrorMessage = "Ooops! The {0} field is necessary to continue...")]
[Display(Name = "Full name")]
public string Name { get; set; }
Notice : depending on
ExpressLocalization
setup, the error messages must be defined in the relevant resource file for DataAnnotations. For more details see Creating Resources.
4.0, 3.2, 3.1, 3.0, 2.0, 1.1, 1.0