Question

So, I have a JSP loop creating select menus when a new record is created. The rule I am trying to accomplish is:

At least one of, could be 50 or more, of these is selected. And that is it. Seems pretty simple to me but I am kinda stuck for some reason...do new rules for each select menu have to be created on the fly? Can I just validate the entire group with 1 rule?

Here is the HTML/JSP:

<select class="reccomendations"
        name="selQuarantineRec"            
        required>
    <option value="">Select...</option>
    <c:forEach items="${qrntRecommendations}" var="qrntRec"
               varStatus="idx">
        <option value="${determination.id}_${ qrntRec.key }">
            <c:out value="${ qrntRec.value.name } "/>
        </option>
    </c:forEach>
</select>

Here is the JS:

ns.util.validate('#nis_pest_form', {
    rules: {
        "selQuarantineRec": { //name of the select elements
            require_from_group: [1, '.reccomendations']
        }
    }
});

Thanks for reading and helping out if you can!
Cheers -b

Was it helpful?

Solution

Quote OP:

"do new rules for each select menu have to be created on the fly?"

Yes. After the plugin is first initialized you can only declare new rules using methods provided by the plugin. See the .rules('add') method.

Create the new element and then call this…

$('#myselect').rules('add', {  // <- select a single element
    require_from_group: [1, '.reccomendations']
});

If you create more than one element at a time, then you must wrap it within a jQuery .each()

$('.select').each(function() {  // <- select a group of elements
    $(this).rules('add', {
        require_from_group: [1, '.reccomendations']
    });
});

NOTE:

If you use the same class selector as for the require_from_group, the code can be condensed to this…

$('.reccomendations').each(function() {
    $(this).rules('add', {
        require_from_group: [1, this]
    });
});

"Can I just validate the entire group with 1 rule?"

No. You must apply the rule to every element in the group.


IMPORTANT:

No matter how you create the elements and declare the rules, each element must contain a unique name attribute. This is how the plugin keeps track of the inputs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top