views:

326

answers:

1

Can Struts2 be used for conditional validation? As in, if checkbox "Other" is filled in, then field "otherDetails" must not be empty?

Note that I'm looking for Struts2, not Struts1. Any example is greatly appreciated.

+1  A: 

You could maybe use JS validation.

JSP:

<s:checkbox id="other" name="other" />
<s:textfield id="otherDetails" name="otherDetails"></s:textfield>

<sx:submit 
            id="button_submit"
            name="button_submit"
            onclick="return checkOther();" />   

JS:

function checkOther() {
    if(document.getElementById('other').checked == true) {
        if(document.getElementyById('otherDetails').value.length == 0) {
            //you should trim the value
            alert("Insert other details");
            return false;
        }
    }

    return true;
}
Trick
Did you mean `<sx:submit` or is that a typo? +1 for general helpfulness, but I'll not accept it since I really want to know if struts validation can do this.
FarmBoy
No, it's not a typo, it's Strutses Dojo tag. For struts validation I hope you'll get an asnwer.
Trick