validation 校验框架(二)-字段校验vs非字段校验

 

在我们的 -validation.xml文件里面可以有两种方式定义校验器(validator):

  1. <validator>
  2. <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 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>

 

这两种是等价的:

<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>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值