在我们的 -validation.xml文件里面可以有两种方式定义校验器(validator):
- <validator>
- <field-validator>
当用其中任何一个是请记住一下几点:
Non-Field-Validator: <validator>允许我们声明两种类型的校验器(a plain Validator或者 a field-specific Validator)
例如:
<validator type="expression>
<param name="expression" > foo gt bar</param>
<message> foo must be great than bar.</message>
</validator>
<validator type="required" >
<param name="fieldName" > bar</param>
<message> You must enter a value for bar.</message>
</validator>
field-validator :(这段说得太长,没看明白。)定义在一个<field-validator>的FieldValidator的filename将会被自动赋予其父节点field的fieldName的值。
建议:<field-validator>和 <validator> (其中的a field-specific Validator),两者选其一时,最好选<field-validator>,因为xml文件好看一些。
注意:<field>里面只能用<field-validator>。
举例:声明一个 FieldValidator
<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>
这两种是等价的:
<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>