fluentvalidation change default error message for int/long

拟墨画扇 提交于 2019-12-24 00:04:54

问题


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?


回答1:


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.




回答2:


You can override that as well.

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



来源:https://stackoverflow.com/questions/9582032/fluentvalidation-change-default-error-message-for-int-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!