Question

Is there a way to change the default error message for an in in FluentValidation?

We are able to set up validations for more complex types but the simple 'the data you entered isn't an int' style things we can't seem to get at.

The built in error for these is: 'the value x isn't valid for y' or something along those lines - is there a way to override these?

Was it helpful?

Solution

There's no easy/clean way to achieve that. The first possibility is to override the DefaultModelBinder.ResourceClassKey property in your application start and point it to a custom resource file:

protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);
    DefaultModelBinder.ResourceClassKey = "Messages";
}

and then define the key PropertyValueInvalid inside App_GlobalResources/Messages.resx.

Another possibility is to use a backing field on your view model as suggested by Jeremy Skinner.

The reason for this is that this error message is generated by the default model binder before any validation can occur on the field. Before you can validate the field it must first be assigned a value. And since you are attempting to convert a string which doesn't represent a valid integer into an integer during model binding, the default model binder assigns a default message.

OTHER TIPS

You can override that as well.

Follow the below link http://fluentvalidation.codeplex.com/wikipage?title=Customising

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top