struts1 validwhen验证

来源:互联网 发布:php视频直播实现 编辑:程序博客网 时间:2024/03/28 20:48

struts1 validwhen验证: 

1.使用validwhen设计复杂的验证

使用validwhen来设计复杂验证的一个经常的要求就是根据一个字段验证另外一个字段(比如, 如果你要用户两次输入口令来确认值口令一致),另外一个就是表单中的一个字段只有另外一个字段有确定值的时候才是必须输入的。新的validwhen验证规则将很快被包含在1.1后的STRUTS版本中,她就是用来处理这种情况的。

validwhen 规则处理单个的变量字段,叫测试。这变量的值是一个布尔的表达式,如果验证有效则它必须为真。可以包含这种变量的表达式有:

单引号或双引号字符串literals

十进制、十六进制、八进制的Integer literals

nullnull和空字符串匹配,

其它可以用属性名引用的form字段,例如customerAge

可以在外部因用得索引字段, 例如childLastName[2]

可以默认implicit因用得索引字段, 例如childLastName[], 她将作为被索引的字段使用同样的索引到数组中,

The literal *这里指它包含当前测试字段的值,

作为例子,考虑一个包含通讯地址和邮箱字段的form。如果通讯地址不为空则邮箱字段是必须的required。你能这样定义validwhen 规则:

<field property="emailAddress" depends="validwhen">

<arg0 key="userinfo.emailAddress.label"/>

<var>

<var-name>test</var-name>

<var-value>((sendNewsletter == null) or (*this* != null))</var-value>

</var>

</field>

上面定义的意思是:如果通讯地址是空或不空时这个字段时有效的。

 

2.使用validwhen验证注意事项:

来源:http://www.nabble.com/Validation-framework...-weird...-td11777141.html

Balazs Michnay wrote:
> Dear Struts Users,
>
> I have a form with 13 form controls on it. I'd like to use the Struts Validation Framework to validate it, but it's acting weird.
> What I'd like to do is to have AT LEAST ONE of the controls filled, but the test of "validWhen" only allows simple boolean expressions.
> I used a simple approach of checking each control for their value being null but the test cannot containmore than one boolean operator.
> For instance, the following works just fine:
>
>                 <var>
>                     <var-name>test</var-name>
>                     <var-value> ((*this* != null) or (suly != null)) </var-value>
>                 </var>
>
> But if I insert another control test, I get an error message saying "ValidWhen Error for field ' magassag' - line 1:37: expecting RPAREN, found 'or'", so no more than two conditions are allowed in a test.
>
> Is that true? Is there any way to create complex tests? Or is there a way to validate the form for having at least one field filled?


The grammar for validwhen expressions is rather limited; you can only
have one operator per group. The solution is to add additional parentheses:

   <var-value>(((*this* != null) or (suly != null)) or ...)/var-value>

原创粉丝点击