struts2 条件 xml 验证
我有一个我认为是常见的问题,但一整天的谷歌搜索没有发现任何有用的东西。
我有一个带有复选框和文本字段的表单。我想对文本字段进行正则表达式验证,但前提是选中了该复选框。正则表达式验证目前适用于其他非条件字段,但我一生都无法弄清楚是否存在允许在 action-validation.xml 文件中执行此操作的语法。 IE。对于其他领域,我有类似下面的内容。我需要的是一种仅在选中复选框时才进行有条件评估的方法。
<validators>
<field name="sn">
<field-validator type="regex" >
<param name="expression">
[0-9]{12}
</param>
<message>Serial number format is invalid. Please try again</message>
</field-validator>
</field>
</validators>
有人有如何执行此操作的代码示例吗?
非常感谢任何帮助。
I have what I'd think is a common issue but an entire day of googling hasn't turned up anything useful.
I have a form with a checkbox and a textfield. I'd like to do a regex validation of the textfield, but only if the checkbox is selected. Regex validation is currently working for other non-conditional fields but I can't for the life of me figure out if there is a syntax that allows for this in the action-validation.xml file. ie. I have something like below for other fields. What I need is a way of making this conditionaly evaluated only if the checkbox is selected.
<validators>
<field name="sn">
<field-validator type="regex" >
<param name="expression">
[0-9]{12}
</param>
<message>Serial number format is invalid. Please try again</message>
</field-validator>
</field>
</validators>
Does anyone have a code example of how to do this?
Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 Struts 1,有一个名为“validwhen”的验证规则,您可以使用它来执行复杂的操作。验证,但对于Struts 2,据我所知没有这样的验证。
对于 Struts 2,您可以使用 表达式验证器,您可以在其中使用可以指定要使用的 OGNL 表达式,所以我想你可以尝试这样的事情:
我不知道的一件事不知道是否有一种简单的方法可以使用 OGNL 进行正则表达式检查(您必须研究它)。
此外,如果这不起作用,您可以随时编写自己的 自定义验证器。
For Struts 1 there was a validation rule called "validwhen" that you could use to perform complex validations, but for Struts 2, as far as I know there is no such validation.
For Struts 2 you could go with the Expression Validator in which you can specify an OGNL expression to use, so I guess you could try something like this:
One thing I don't know is if there is a easy way of doing your regular expression check using OGNL (you'll have to look into it).
Additionally, if that does not work, you can always write your own custom validator.
表达式 和字段表达式 验证器采用 OGNL 表达式。评估为 true 则通过, false 则失败。您可以很容易地使用它们来满足此要求。
就我个人而言,我只会像您一样通过 XML 使用内置验证器,然后在 validate() 方法(或 methodname-validate() 方法)中实现任何自定义验证逻辑。只是偏好,没有什么充分的理由。
The expression and fieldexpression validators take an OGNL expression. Evaluating to true passes false fails. You can use them quite easily for this requirement.
Personally I would just use the built in validators via XML like you're doing and then implement any custom validation logic in the validate() method (or methodname-validate() method). Just preference, no good reason really.