I\'m trying to validate select2 field using jquey.validation plugin but nothing happens.
I want to make select required field.
I\'m using this custom validat
According to the error placement logic of jQuery validator, you may use the markup like below.
<div class="input-group col-sm-12">
<select class="form-control select2" id="ELEMENT ID" name="ELEMENT NAME">
</select>
</div>
It will place the error block below the select2 box and make the error block visible.
When you create a custom rule, you also must properly declare it. Since your custom rule depends on an argument, then you must pass the argument when declaring the rule
rules: {
country: {
required: true,
requiredcountry: "your argument here" // <- declare the custom rule
}
}
Just add ignore: [],
in Your form validation function. It will enable hidden field validation.So You will get validation for Select 2
$("#ContactForm").validate({
ignore: [],
rules: {
//Rules
},
messages: {
//messages
}
});
You can check this link: https://github.com/rkeshmir/select2-validation
Here you can find the simplest way to validate select2 components. The idea id trigger validation for all <select>
items in DOM on their change event. Also I have added required CSS rules to highlight error and success states of input.
To validate 'select2' inputs you first must tell jquery validate to consider hidden elements:
$.validator.setDefaults({
ignore: [],
});
This is custom highlight / unhighlight that i use for pointing select2 and tagit errors with jquery validate:
$("#someform").validate({
rules: {
nazwa: {
required: true
},
ilosc_w_opakowaniu: {
required: "#czy_opakowanie_zbiorcze:checked"
}
},
messages: {
nazwa: "Musisz wypełnić pole [Nazwa]",
ilosc_w_opakowaniu: "Musisz podać ilość produktu w opakowaniu zbiorczym",
},
highlight: function( element, errorClass, validClass ) {
if ( element.type === "radio" ) {
this.findByName( element.name ).addClass( errorClass ).removeClass( validClass );
} else {
$( element ).addClass( errorClass ).removeClass( validClass );
}
// select2
if( $(element).hasClass('select2-hidden-accessible') ){
dzik = $(element).next('span.select2');
if(dzik)
dzik.addClass( errorClass ).removeClass( validClass );
}
// tagit
dzik2 = $(element).parent().find('ul.tagit')
if( dzik2.length == 1 )
dzik2.addClass( errorClass ).removeClass( validClass );
},
unhighlight: function( element, errorClass, validClass ) {
if ( element.type === "radio" ) {
this.findByName( element.name ).removeClass( errorClass ).addClass( validClass );
} else {
$( element ).removeClass( errorClass ).addClass( validClass );
}
// select2
if( $(element).hasClass('select2-hidden-accessible') ){
dzik = $(element).next('span.select2');
if(dzik)
dzik.removeClass( errorClass ).addClass( validClass );
}
// tagit
dzik2 = $(element).parent().find('ul.tagit')
if( dzik2.length == 1 )
dzik2.removeClass( errorClass ).addClass( validClass );
}
});
And some CSS:
/** select2 error hightight **/
.select2.error .select2-selection--single{
border-color:red !important;
color:red !important;
}
/** tagit error highlight **/
.tagit.error{
border-color:red !important;
color:red !important;
}
I've found that getting Select2 to work with jQuery Validate very much depends on the markup used in creating the Select2 control.
For the most practical use of the plugin, you'll probably be using dynamically loaded data (data obtained via web service vs. static <option/>
elements in the page). And per their spec, the way to accomplish this is by attaching Select2 to an <input type="hidden"/>
element. However, elements of type="hidden" are not validated by jQuery Validate by default.
In order to validate hidden elements using jQuery Validate, one must tinker with the ignore
property in jQuery Validate's configuration object. See the ignore
option in their spec. Try initializing the ignore
value as null
to get the validation to trigger.