I can test multiple selections with jquery-validate and created a fiddle as a demo. Remove the selection from the selected object by holding Ctrl and click on the selection to see the effect.
<form id="myform"> <select id="id_deals-1-sales_item" class="multi_select_mandatory" name="deals-1-sales_item" multiple="multiple"> <option value="1">Hotel 3 Star</option> <option selected="selected" value="2">Hotel 4 Star</option> </select> </form> $(document).ready(function() { var validator = $('#myform').validate({
But as soon as I select the multi selector, it stops working: see fiddle .
$('#id_deals-1-sales_item').chosen();
During research, I found that someone tried this instead of jquery multiselect instead of the selected one. Hidden elements seem to be ignored when checking jquery. I tried to apply this solution, but since Chosen had different methods, I was stuck (multiselection does not exist in the selected)
Is there any jQuery guru that could point me in the right direction? In addition, I would prefer a solution based on classes rather than based on field names. Like this:
This is the solution that I came up halfway through. But I donβt know how to continue ??? .
$.validator.addMethod("needsSelection", function(value, element) { return $(element).???.length > 0; }); var validator = $('#myform').validate({ }); $('#myform').find('select.multi_select_mandatory').each(function(){ $(this).change(function(){ $(this).valid(); }); $(this).rules('add', { needsSelection: "" }); });
Decision:
With the eicto solution below, I was able to create a class based instead of a solution based on a field name. This is especially useful when you have dynamic elements that you want to check immediately without sending anything to the server.
var validator = $('#deal_modal_form').validate({ // options ignore: ':hidden:not(.chzn-done)' }); $('#myform').find('select.multi_select_mandatory').each(function(){ $(this).chosen().change(function(){ $(this).valid(); }); $(this).rules('add', { required: true, }); });