I get the validation message "The value xxx is not valid for yyy". It happens when I post incorrect value for double type. I have no idea how to change it.
Asked
Active
Viewed 2,218 times
2 Answers
7
Unfortunately this isn't something that FluentValidation has the ability to override - the extensibility model for MVC's validation is somewhat limited in many places, and I've not been able to find a way to override this particular message.
An alternative approach you could use is to define two properties on your view model - one as a string, and one as a nullable double. You would use the string property for MVC binding purposes, and the double property would perform a conversion (if it can). You can then use this for validation:
public class FooModel {
public string Foo { get; set; }
public double? ConvertedFoo {
get {
double d;
if(double.TryParse(Foo, out d)) {
return d;
}
return null;
}
}
}
public class FooValidator : AbstractValidator<FooModel> {
public FooValidator() {
RuleFor(x => x.ConvertedFoo).NotNull();
RuleFor(x => x.ConvertedFoo).GreaterThan(0).When(x => x.ConvertedFoo != null);
}
}

Jeremy Skinner
- 1,411
- 1
- 9
- 7
-
Did you try to contact with ASP.NET MVC team about it? – SiberianGuy Sep 15 '11 at 16:01
-
Yes, I raised it several times during the preview period for MVC2, but it was never changed. – Jeremy Skinner Sep 16 '11 at 21:28
-
Let's try it once more: http://forums.asp.net/p/1721550/4601626.aspx/1?p=True&t=634518558226229075 – SiberianGuy Sep 17 '11 at 15:30
-
As war as I know, this is not even MVC. Those messages are deep down in the .net framework.See https://stackoverflow.com/questions/6587816/how-to-change-the-errormessage-for-int-model-validation-in-asp-net-mvc – yonexbat Mar 03 '18 at 07:23
-
this error still going in netcore, after all these years, we need to use this workaround? or there is another approach to solve this? – RokumDev Jun 18 '20 at 20:48
0
You could use the .WithMessage()
method to customize the error message:
RuleFor(x => x.Foo)
.NotEmpty()
.WithMessage("Put your custom message here");
and if you want to use localized messages with resources:
RuleFor(x => x.Foo)
.NotEmpty()
.WithLocalizedMessage(() => MyLocalizedMessage.FooRequired);

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
2It doesn't work. The error appears when ASP.NET tries to convert string to double – SiberianGuy Sep 15 '11 at 06:35