jsp - How to validate field conditionally using Struts 2 Validation? -
i newbie on struts2 , sorry poor english not mother language.
assume form has 1 multiple select box , 3 text fields:
multiple select box: name: sb01
, options: [value, label]
: [1, type1], [2, type2], [3, type3]
<s:select name="sb01" multiple="multiple" list="#{1:'type1', 2:'type2',3:'type3'}"></s:select>
3 text fields: (textfield1
, textfield2
, textfield3
)
<s:textfield name="textfield1" /> <s:textfield name="textfield2" /> <s:textfield name="textfield3" />
if type1 selected, user needs input textfield1.
if type2
selected, user needs input textfield2
.
if type3 selected, user needs input textfield3.
if type1,2,3 selected, user needs input textfields. on , forth.
how can textfields validated conditionally? using xml or annotation instead of writing validate() method in action class because code long reading , maintaining when fields grow.
i have done below logic incorrect still requires 3 options selected.
@fieldexpressionvalidator(expression = "sb01.contain(1)", message="") @requiredstringvalidator(message = "please fill in textfield1.") public string gettextfield1(){ return textfield1; } @fieldexpressionvalidator(expression = "sb01.contain(2)", message="") @requiredstringvalidator(message = "please fill in textfield2.") public string gettextfield2(){ return textfield2; } @fieldexpressionvalidator(expression = "sb01.contain(3)", message="") @requiredstringvalidator(message = "please fill in textfield3.") public string gettextfield3(){ return textfield3; }
you shouldn't use field validators, because field validator applied on field annotated it. , if have 3 field validators of them applied on each field during validation process. instead can use 1 non-field validator validated 3 fields simultaneously. setting dynamically field required doesn't tell struts or adjust reconfigure validators. none of them should marked required because can remove mark of each one.
Comments
Post a Comment