这里有两种方法在你的-validation.xml文件里定义校验器
1.<validator>
2.<field-validator>
当你使用这两种方法时请将下面的禁忌于心。
非字段校验器:你可以使用<validator>标签定义两种类型的校验器(抽象出来的校验器或具体字段的校验器)
view plaincopy to clipboardprint?
01.---------------------------这个没有绑定到具体的字段
02.<validator type="expression>
03. <param name="expression">foo gt bar</param>
04. <message>foo must be great than bar.</message>
05.</validator>
06.---------------------------这个绑定到了bar字段
07.<validator type="required">
08. <param name="fieldName">bar</param>
09. <message>You must enter a value for bar.</message>
10.</validator> ---------------------------这个没有绑定到具体的字段
<validator type="expression>
<param name="expression">foo gt bar</param>
<message>foo must be great than bar.</message>
</validator>
---------------------------这个绑定到了bar字段
<validator type="required">
<param name="fieldName">bar</param>
<message>You must enter a value for bar.</message>
</validator>
字段校验器:<field-validator>和<validator>只有一点不同, 那就是<field-validator>的fieldName属性继承自他的父标签<field>标签。
<field>标签通过制定name属性为他的子标签绑定该指定字段,如:<field name="email_address"> 。
采用这种结构的好处,很容易把针对某一个字段的校验器集合到一个校验器组,否则的话你每个<validator>标签都要写一个name属性,我的天啊。
在<field>标签下定义<field-validator>标签比在每一个<validator>标签下定义fieldname参数更清晰。(字段组更清晰)
注意:你只能在field标签里面定义<field-validator>元素,而不能定义抽象的<validator>元素。
如果你在<field>标签里面定义<validator>将导致错误,因为这在 xwork-validator-1.0.2.dtd是不允许的 在定义
字段校验器时请遵循以下语法规则:
view plaincopy to clipboardprint?
01.<field name="email_address">
02. <field-validator type="required">
03. <message>You cannot leave the email address field empty.</message>
04. </field-validator>
05. <field-validator type="email">
06. <message>The email address you entered is not valid.</message>
07. </field-validator>
08.</field> <field name="email_address">
<field-validator type="required">
<message>You cannot leave the email address field empty.</message>
</field-validator>
<field-validator type="email">
<message>The email address you entered is not valid.</message>
</field-validator>
</field>
你可以选择之一。你使用非字段校验,并且每一次都为他设定fieldname属性是完全合法的。下面两种方式是一样的。
view plaincopy to clipboardprint?
01.<field name="email_address">
02. <field-validator type="required">
03. <message>You cannot leave the email address field empty.</message>
04. </field-validator>
05. <field-validator type="email">
06. <message>The email address you entered is not valid.</message>
07. </field-validator>
08.</field>
09.
10.<validator type="required">
11. <param name="fieldName">email_address</param>
12. <message>You cannot leave the email address field empty.</message>
13.</validator>
14.<validator type="email">
15. <param name="fieldName">email_address</param>
16. <message>The email address you entered is not valid.</message> 17.</validator> <field name="email_address">
<field-validator type="required">
<message>You cannot leave the email address field empty.</message>
</field-validator>
<field-validator type="email">
<message>The email address you entered is not valid.</message>
</field-validator>
</field>
<validator type="required">
<param name="fieldName">email_address</param>
<message>You cannot leave the email address field empty.</message>
</validator>
<validator type="email">
<param name="fieldName">email_address</param>
<message>The email address you entered is not valid.</message>
</validator>
字段校验器:一个<field>绑定一个字段,一个<field>可以有多个<field-validator>,对一个字段可以有多个校验。
结构清晰,方便浏览和检查。易于维护。
非字段校验器:一个<validator>标签下面只能对一个字段进行校验,如果对一个字段进行多次校验,需要写很多个<validator>,很繁琐。而且不利于维护。
非字段校验器vs字段校验器
最新推荐文章于 2024-03-27 01:32:08 发布