views:

56

answers:

1

I have a table (in a form) populated with radio buttons (with a button for each value in a collection). If the collection is empty, nothing shows up in the table (which is fine). (I'm using Struts2)

My trouble comes when validating that the user has selected one of these radio buttons when the submit button is clicked. I'm using JQUERY validations, and it works quite well UNLESS there are no radio buttons to select (collection is empty).

$('#startProcessForm').validate({ 
    rules: { 
        selectedProcess: {
            required: true
        } 
    }, 
    messages: { 
        selectedProcess: "Please select a process to start." 
    } 
}); 

If the list IS empty, then there are no fields (radio button or otherwise) named "selectedProcess". So my question is this:

How can I make "selectedProcess" exist AND fail the 'required:true' validation if there's nothing in the collection?

I was thinking I could create an empty tag of the same name (selectedProcess) if the collection is empty? Here's what I'd guess would work but doesn't seem to:

<s:if test="processes != null && !processes.isEmpty()">
    <s:iterator value="processes" status="processesStatus">
        <tr>
            <td><s:radio name="selectedProcess" list="{name}"></s:radio></td>
        </tr>
    </s:iterator>
</s:if>
<s:else>
    <tr>
        <td><a name="selectedProcess"></a></td>
    </tr>
</s:else>

Thanks!

+2  A: 

you could do <input type="hidden" name="selectedProcess" value="-1"> if there are no values (instead of your <a name>)

oedo
Great - thank you! Unfortunately though, -1 as a value didn't fail the validation. However, I just tried [value=""], and it works very well. Thanks oedo!
jcovert