How to customize the default .Net Framework Validation Error Messages
-
I have in my view model the following code:
public DateTime? Activation_Date { get; set; }
public DateTime? Deactivation_Date { get; set; }On the form, if I put the date as "abc" and click Save, I get the following validation error:
The value 'abc' is not valid for Activation_Date.
What I simply want is to modify Activation_Date. I want it to be "Activation Date" so the message will look like below. The end user doesn't want the underscore.
The value 'abc' is not valid for Activation Date.
View Code to display error messages
@Html.ValidationMessageFor(s => s.Activation_Date)
What is the technique to make this kind of change ?
-
I have in my view model the following code:
public DateTime? Activation_Date { get; set; }
public DateTime? Deactivation_Date { get; set; }On the form, if I put the date as "abc" and click Save, I get the following validation error:
The value 'abc' is not valid for Activation_Date.
What I simply want is to modify Activation_Date. I want it to be "Activation Date" so the message will look like below. The end user doesn't want the underscore.
The value 'abc' is not valid for Activation Date.
View Code to display error messages
@Html.ValidationMessageFor(s => s.Activation_Date)
What is the technique to make this kind of change ?
The ValidationMessageFor extension method has an override that accepts a custom validation message as the second parameter.
@Html.ValidationMessageFor(s => s.Activation_Date, "Where we're going Marty, we don't need roads");
-
The ValidationMessageFor extension method has an override that accepts a custom validation message as the second parameter.
@Html.ValidationMessageFor(s => s.Activation_Date, "Where we're going Marty, we don't need roads");
Hi Pete, It doesn't work for me. @Html.ValidationMessageFor(s => s.Activation_Date, "Activation Date is invalid"); 1. I always have "Activation Date is invalid" visible beneath the activation date input field 2. Secondly, I have other validation error messages in a Validator class for Activation_Date field
RuleFor(x => x.Activation_Date).NotEmpty().WithMessage("Activation Date is required");
RuleFor(x => x.Activation_Date).GreaterThanOrEqualTo(DateTime.Today).WithMessage("Activation Date must be greater than or equal to the current date"); -
Hi Pete, It doesn't work for me. @Html.ValidationMessageFor(s => s.Activation_Date, "Activation Date is invalid"); 1. I always have "Activation Date is invalid" visible beneath the activation date input field 2. Secondly, I have other validation error messages in a Validator class for Activation_Date field
RuleFor(x => x.Activation_Date).NotEmpty().WithMessage("Activation Date is required");
RuleFor(x => x.Activation_Date).GreaterThanOrEqualTo(DateTime.Today).WithMessage("Activation Date must be greater than or equal to the current date");You appear to be mixing FluentValidation with ASP MVC Validation. I would choose one and stick with that. In this case, FluentValidation is going to be more flexible for you.