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