Because the validation of the form does not validate hidden elements, you have to indicate that you have to validate those hidden elements with the ignore:[]
property.
Later this would validate the select
but every time it changes it would not validate again and that is a problem for that you have to apply the validation every time a change is made in select
.
$select.on('change', function() {
$(this).trigger('blur');
});
Once we have these changes, we add a rule to the plugin so that it validates the select2
since it does not have the attribute name
.
$select.rules('add', {
required: true,
messages: {
required: "Seleccione una Opcion"
}
});
Note:
This can have problems when using Ajax in the select2.
I leave you an example working
(function() {
var $select = $('select').select2({
placeholder: 'Choose',
allowClear: true
});
/*
* Aplicando la validacion del select cada vez que cambie
*/
$select.on('change', function() {
$(this).trigger('blur');
});
/*
* Permitiendo la validacion de campos ocultos
*/
$('#myForm').validate({
ignore: '.select2-input, .select2-focusser',
submitHandler: function(form) {
alert("enviado")
},
errorPlacement: function(error, element) {
$(element).next().append(error);
}
});
/*
* agregando la validacion del select
* ya que no tiene un atributo name el plugin
*/
$select.rules('add', {
required: true,
messages: {
required: "Seleccione una Opcion"
}
});
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<form id="myForm">
<select name="country" id="country" style="width:200px">
<option></option>
<option value="US">United States</option>
<option value="CA">Canada</option>
</select>
<br/>
<button type="submit">Go</button>
</form>
This is an example with ajax
taking as an example the one from the Select2 page
function formatRepo(repo) {
if (repo.loading) return repo.text;
var markup = "<div class='select2-result-repository clearfix'>" +
"<div class='select2-result-repository__avatar'><img src='" + repo.owner.avatar_url + "' /></div>" +
"<div class='select2-result-repository__meta'>" +
"<div class='select2-result-repository__title'>" + repo.full_name + "</div>";
if (repo.description) {
markup += "<div class='select2-result-repository__description'>" + repo.description + "</div>";
}
markup += "<div class='select2-result-repository__statistics'>" +
"<div class='select2-result-repository__forks'><i class='fa fa-flash'></i> " + repo.forks_count + " Forks</div>" +
"<div class='select2-result-repository__stargazers'><i class='fa fa-star'></i> " + repo.stargazers_count + " Stars</div>" +
"<div class='select2-result-repository__watchers'><i class='fa fa-eye'></i> " + repo.watchers_count + " Watchers</div>" +
"</div>" +
"</div></div>";
return markup;
}
function formatRepoSelection(repo) {
return repo.full_name || repo.text;
}
(function() {
var $select = $('select').select2({
placeholder: 'Choose',
allowClear: true,
ajax: {
url: "https://api.github.com/search/repositories",
dataType: 'json',
delay: 250,
data: function(params) {
return {
q: params.term, // search term
page: params.page
};
},
processResults: function(data, params) {
// parse the results into the format expected by Select2
// since we are using custom formatting functions we do not need to
// alter the remote JSON data, except to indicate that infinite
// scrolling can be used
params.page = params.page || 1;
return {
results: data.items,
pagination: {
more: (params.page * 30) < data.total_count
}
};
},
cache: true
},
escapeMarkup: function(markup) {
return markup;
},
minimumInputLength: 1,
templateResult: formatRepo,
templateSelection: formatRepoSelection
});
/*
* Aplicando la validacion del select cada vez que cambie
*/
$select.on('change', function() {
$(this).trigger('blur');
});
/*
* Permitiendo la validacion de campos ocultos
*/
$('#myForm').validate({
ignore: '.select2-input, .select2-focusser',
submitHandler: function(form) {
alert("enviado")
},
errorPlacement: function(error, element) {
$(element).next().append(error);
}
});
/*
* agregando la validacion del select
* ya que no tiene un atributo name el plugin
*/
$select.rules('add', {
required: true,
messages: {
required: "Seleccione una Opcion"
}
});
}());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<form id="myForm">
<select name="country" id="country" style="width:200px">
<option></option>
<option value="US">United States</option>
<option value="CA">Canada</option>
</select>
<br/>
<button type="submit">Go</button>
</form>