Jquery validations on form with no fields

Posted by jcovert on Stack Overflow See other posts from Stack Overflow or by jcovert
Published on 2010-03-30T15:48:31Z Indexed on 2010/03/30 16:13 UTC
Read the original article Hit count: 414

Filed under:
|
|
|
|

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!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about validations