views:

394

answers:

1
+2  Q: 

struts2 validation

I have a text field and a selectbox (users can select multiple values by ctrl) in a form.

I was reading validation provided by struts2: http://struts.apache.org/2.x/docs/validation.html

However, it doesnt seem to have the validation I need.

I want to make the text field a required field ONLY when certain strings are selected from the selectbox.

Do I have to write a custom validator for this purpose or is there a simpler way to achieve this in struts2.

thanks!

PS: I would like to know how other languages/frameworks might handle this case also.

+2  A: 

Expression or field expression caters for checking fields dependent on other fields. For example, to check for a confirmed email address you could do the following:

<field name="confirmAddress">
    <field-validator type="fieldexpression">
        <param name="expression">address == confirmAddress</param>
        <message key="nomatch"/>
    </field-validator>
</field>

Creating a custom validator is trivial though, and easy to reuse.

Pool