Spring“类型不匹配”和必填字段

发布于 2024-09-17 10:25:59 字数 456 浏览 2 评论 0原文

在 Spring Webflow 2.0.x 的上下文中......

我处理表单绑定“typemismatches”,即尝试将字符串映射到整数字段的结果,通过在我的messages.properties中使用以下内容

typeMismatch={0} contains invalid data.

这工作得很好。

问题是,如果发生 typeMismatch 错误的字段是“必填”,那么我也会收到缺少必填字段的错误,我猜这是合乎逻辑的,因为提交的值从未绑定。 (“必需”在 Commons Validation XML 文件中定义)

因此,当字段仅由于类型不匹配而丢失时,我不想看到“XXX 是必填字段”错误消息。我该如何解决这个问题?我考虑过重写 FormAction 上的 initBinder() 但很快就无济于事......

In the context of Spring Webflow 2.0.x......

I handle form binding "typemismatches", i.e. as a result of trying to map a String onto a Integer field, by using the following in my messages.properties

typeMismatch={0} contains invalid data.

This works fine.

The problem is that if the field that the typeMismatch error occurred on was "required" then I also receive an error for the missing required field, which is logical I guess because the value that was submitted was never bound. ("Required" being defined in a Commons Validation XML file)

So, I dont want to see the "XXX is required field" error message when the field is only missing due to the typeMismatch. How do I resolve this? I thought about overriding initBinder() on the FormAction but quickly got nowhere.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2024-09-24 10:25:59

就像 Yves 提到的,在这三种方法中,我使用了自定义验证器方法,它非常简单。您可以使用自定义验证器来检查表单字段是否已包含必需的 xml 错误消息。如果该字段没有错误,那么您可以检查字符串验证。这样它将只显示一个。

您可以使用的另一种方法是尝试多重 xml 验证,其中一个是必需的,另一个是检查特定正则表达式的掩码。在您的情况下,如果您的字段是整数字段,那么您可以使用正则表达式仅检查数字来执行掩码。 xml 中 mask、required 或 required、mask 的顺序决定了哪条消息获得更高的优先级。

例如:

<field property="somefield" depends="required,mask" page="2">
<arg key="somelabel"/>
<var>
    <var-name>mask</var-name>
    <var-value>${somepattern}</var-value>
</var>
</field>

Like Yves mentioned, among the three approaches, i have used a custom validator method and its very easy. You can use a custom validator which checks if the form field already has a xml error message of required. If the field does not have an error, then you can check for your string validation. That way it will display only one.

The other method that you could use is try a multiple xml validation, one being required and the other one being a mask which checks for a particular regular expression. In your case if your field is an integer field, then you can go and perform a mask with regex checking for only numbers. The order of mask, required or required, mask in the xml decides which message gets a higher preference.

For example:

<field property="somefield" depends="required,mask" page="2">
<arg key="somelabel"/>
<var>
    <var-name>mask</var-name>
    <var-value>${somepattern}</var-value>
</var>
</field>
神爱温柔 2024-09-24 10:25:59

您有多种选择,按优先顺序排列:

  • 在资源文件中选择性设置消息 typeMismatch.target.yourFieldNametypeMismatch.int

  • 实现您的自己的验证器,以便您可以发送专用消息当整数解析在绑定步骤之前失败

  • 创建一个BindingErrorProcessor来处理不同的各种解析问题

You have many options, in order of preference:

  • Set selectively the message typeMismatch.target.yourFieldName or typeMismatch.int in resources files

  • Implement your own Validator so that you can send a dedicated message when Integer parsing will fail before the binding step

  • Create a BindingErrorProcessor to handle different kind of parsing issues

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文