1

I would like to validate that a Struts2 checkbox is selected (boolean property) but the validation always fails at the moment. here is what I have:

<s:checkbox key="agreetc" fieldValue="agreedtc"
            label="I have read and agree to the terms and conditions"/>

<field name="agreetc">
    <field-validator type="fieldexpression">
        <param name="expression">agreetc == true</param>
        <message>You must agree to the terms and conditions</message>
    </field-validator>
</field>

The value of agreetc in the action is always false because the setter for the field never gets called. If I add a String called test with a getter/setter and a s:textfield then that value is picked but I can't get the values from s:radio, s:select or s:checkbox in this action. Other actions I have been successful with s:radio and s:select.

Does anyone have any ideas?

Doahh
  • 590
  • 1
  • 8
  • 20
  • Looks fine by me. What do you mean fails? Always true or false or something else....? – Aleksandr M Aug 15 '13 at 17:23
  • I have been looking at this a little more and it seems that Struts2 never receives any value for the checkbox at all - it is always null which will be why the validation is failing. In the action I have 'private boolean agreedtc;' with its getter and setter. The action pick up several other form field without issue but not the agreetc field. – Doahh Aug 15 '13 at 17:35
  • Sorry, it is always `false`, please ignore my previous comment. – Doahh Aug 15 '13 at 17:41
  • Doahh, don't post the code in comments, edit you question instead and add details. – Roman C Aug 15 '13 at 17:51

1 Answers1

1

If you want to receive value as String from fieldValue attribute then make your agreedtc variable String type not boolean. Then your validation as it is now should work.

But if you want it to be a boolean in your action class then remove the fieldValue attribute from <s:checkbox> tag and change your validation expression to:

<field name="agreetc">
    <field-validator type="fieldexpression">
        <param name="expression">agreetc</param>
        <message>You must agree to the terms and conditions</message>
    </field-validator>
</field>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • This works correctly, but I also stupidly has _agreetc_ in the JSP and _agreedtc_ as the action property which didn't help! Thank you to all who commented. – Doahh Aug 16 '13 at 01:35
  • @Doahh Why did you accept it if it didn't solve your problem? – Roman C Aug 16 '13 at 09:12
  • Why do you feel the urge to downvote an answer if the bug was in the question ? – Andrea Ligios Aug 16 '13 at 16:57
  • @AndreaLigios: I believe it is retaliation downvote... Because someone thinks I downvoted his other question, which I DIDN'T. http://stackoverflow.com/q/18270644/1700321 – Aleksandr M Aug 16 '13 at 17:28
  • @Roman C I said it did fix the problem but there was also another problem that I needed to fix to be able to see that the given accepted solution was in fact the correct one. Aleksandr M is correct, if I only fixed the typo in the action property the validation would still fail without the solution you provided - thanks again. – Doahh Aug 17 '13 at 16:20