I have some inputs using the chosen plugin that I want to validate as \"required\" on the client side. Since \"chosen\" hides the actual select e
jQuery("#formID").validationEngine({
prettySelect : true,
useSuffix: "_chzn"
});
jQuery-Validation-Engine/demoChosenLibrary
My form includes conditionally hidden fields, to prevent the hidden chosen fields failing validation I've extended the default ignore:hidden a little further:
$.validator.setDefaults({
ignore: ":hidden:not(.chosen-select + .chosen-container:visible)" //for all select having class .chosen-select
})
I think this is the better solution.
//trigger validation onchange
$('select').on('change', function() {
$(this).valid();
});
$('form').validate({
ignore: ':hidden', //still ignore Chosen selects
submitHandler: function(form) { //all the fields except Chosen selects have already passed validation, so we manually validate the Chosen selects here
var $selects = $(form).find('select'),
valid = true;
if ($selects.length) {
//validate selects
$selects.each(function() {
if (!$(this).valid()) {
valid = false;
}
});
}
//only submit the form if all fields have passed validation
if (valid) {
form.submit();
}
},
invalidHandler: function(event, validator) { //one or more fields have failed validation, but the Chosen selects have not been validated yet
var $selects = $(this).find('select');
if ($selects.length) {
validator.showErrors(); //when manually validating elements, all the errors in the non-select fields disappear for some reason
//validate selects
$selects.each(function(index){
validator.element(this);
})
}
},
//other options...
});
Note: You will also need to change the errorPlacement
callback to handle displaying the error. If your error message is next to the field, you will need to use .siblings('.errorMessageClassHere')
(or other ways depending on the DOM) instead of .next('.errorMessageClassHere')
.
jQuery validate ignores the hidden element, and since the Chosen plugin adds visibility:hidden
attribute to the select, try:
$.validator.setDefaults({ ignore: ":hidden:not(select)" }) //for all select
OR
$.validator.setDefaults({ ignore: ":hidden:not(.chosen-select)" }) //for all select having class .chosen-select
Add this line just before validate()
function. It works fine for me.
Thanks this answer https://stackoverflow.com/a/40310699/4700162 i resolve the iussue:
Inside the file the Chosen.jquery.js, change
this.form_field_jq.hide().after(this.container);
with this:
this.form_field_jq.css('position', 'absolute').css('opacity', 0).after(this.container);
@Anupal put me on the right path but somehow I needed a complex fix
.chosen
to be consideredjavascript
$.validator.setDefaults({ ignore: ":hidden:not(.chosen)" })
Or any other name you give to your chosen's. This is global configuration. Consider setting on top level
Thanks to BenG
html
<select id="field" name="field" class="chosen" data-rule-chosen-required="true">
<option value="">Please select…</option>
</select>
javascript
Again, global configuration for $.validator
object. Can be put next to the previous command
$.validator.addMethod('chosen-required', function (value, element, requiredValue) {
return requiredValue == false || element.value != '';
}, $.validator.messages.required);