Struts validator of field conditional to another variable on the jsp

[亡魂溺海] 提交于 2019-12-13 08:49:50

问题


I'm using the struts validator for a jsp validation and i want to know how can I define a field validation depending on the value of another field of my jsp.

I have a list of elements like this one :

<s:select name="varName" label="labelVariable" list="listeVariable" listValue="varValue" listKey="varKey"/>

And a textfield like this one :

<s:textfield name="varNameText" label="labelText"/>

In my validator I want to validate the size of the field 'varNameText' depending on the selected value on the list.

If my list has the values 1/2/3, I want to validate :

  • If list = 1 the varNameText lenght should be 10
  • If list != 1 the varNameText lenght should be 12

How can I do this into my xml file.

Thanks for the answers.


回答1:


You can use fieldExpression validator

Validates a field using an OGNL expression.

You can find an example of fieldExpression validator here.

The point that you can create an expression that using any number of fields populated to action bean before you run the validation against the valueStack.

For example

<field-validator type="fieldexpression">
  <param name="expression"><![CDATA[varName==1?(varNameText.length()==10):(varNameText.length()==12)]]></param>
  <message>varNameText length should be ${varName==1?10:12}</message>
</field-validator>


来源:https://stackoverflow.com/questions/45816010/struts-validator-of-field-conditional-to-another-variable-on-the-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!