jquery validate errorPlacement in select2.js [duplicate]
Asked Answered
W

1

5

When error replacement in select2 is showing up above the box, I want the error replacement below the box. Here is my HTML:

<form id="form" class="form_required">
    <select type="text" id="test1" class="form-control select2" required>
        <option value="">&nbsp();</option>
        <option value="1">1</option>
    </select>
    <button type="button" onclick="insert()">Submit</button>
</form>

Here is the JavaScript:

$('.select2').select2()

$(".form_required").validate({
    highlight: function (element, errorClass, validClass) {
        $(element).parents('.form-control').removeClass('has-success').addClass('has-error');     
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).parents('.form-control').removeClass('has-error').addClass('has-success');
    },
    errorPlacement: function (error, element) {
        if (element.parent('.input-group').length) {
            error.insertAfter(element.parent());
        }
        else if (element.prop('type') === 'radio' && element.parent('.radio-inline').length) {
            error.insertAfter(element.parent().parent());
        }
        else if (element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
            error.appendTo(element.parent().parent());
        }
        else {
            error.insertAfter(element);
        }
    }
});

function insert(){
    if ($('#form').valid()){
        alert('OK');
    }
}

1) enter image description here 2) enter image description here

When not using select2 the error replacement is showing up below box the like number 2 but when using select2 error replacement is showing up above the box..

Wetmore answered 7/8, 2018 at 9:59 Comment(2)
jsfiddle.net/5f0r9vsy/86 this exampleWetmore
Integrating jQuery Validate with Select2 has been asked and answered before. Please use the search.Unwrap
R
19

If you check the source code of the select2 library, you'll see that the select2-container (container responsible for the select2 dropdown) is inserted right after the select element. Here's the part of the code which does that:

Select2.prototype._placeContainer = function ($container) {
 $container.insertAfter(this.$element);

 var width = this._resolveWidth(this.$element, this.options.get('width'));

 if (width != null) {
  $container.css('width', width);
 }
};

which gets us to easily place the error message from the jquery-validate after this select2-container. Here's the change in the errorPlacement option:

errorPlacement: function (error, element) {
    if(element.hasClass('select2') && element.next('.select2-container').length) {
        error.insertAfter(element.next('.select2-container'));
    }
    ....

And a snippet showing the results:

$('.select2').select2({container: 'body'})

$(".form_required").validate({
    highlight: function (element, errorClass, validClass) {
        $(element).parents('.form-control').removeClass('has-success').addClass('has-error');     
    },
    unhighlight: function (element, errorClass, validClass) {
        $(element).parents('.form-control').removeClass('has-error').addClass('has-success');
    },
    errorPlacement: function (error, element) {
    		if(element.hasClass('select2') && element.next('.select2-container').length) {
        	error.insertAfter(element.next('.select2-container'));
        } else if (element.parent('.input-group').length) {
            error.insertAfter(element.parent());
        }
        else if (element.prop('type') === 'radio' && element.parent('.radio-inline').length) {
            error.insertAfter(element.parent().parent());
        }
        else if (element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
            error.appendTo(element.parent().parent());
        }
        else {
            error.insertAfter(element);
        }
    }
});
form {
  margin: 20px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css">

<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.full.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.min.js"></script>

<form id="form" class="form_required form-horizontal">    

<div class="form-group">
<div class="col-sm-8">

    <select type="text" id="test1" class="form-control select2" required>
        <option value="">&nbsp;</option>
        <option value="1">test1</option>
    </select>
</div>
</div>

<div class="form-group">
    <button type="button" onclick="insert()">Submit</button>
</div>
</form>

<form id="form2" class="form_required form-horizontal">    

<div class="form-group">
    <div class="col-sm-8">
    <select type="text" id="test2" class="form-control" required>
        <option value="">&nbsp;</option>
        <option value="1">test2</option>
    </select>
    </div>
</div>

<div class="form-group">
    <button type="button" onclick="insert2()">Submit</button>
</div>
</form>

<script>
function insert(){
    if ($('#form').valid()){
        alert('OK');
    }
}

function insert2(){
    if ($('#form2').valid()){
        alert('OK');
    }
}
</script>

Hope this helps.

Relativistic answered 7/8, 2018 at 14:8 Comment(4)
thank you so much @RelativisticWetmore
You're welcome. Glad I could help.Relativistic
helped me a lot. thanksCowled
Thanks for help just want to give what i have to change in the code : I was not able to find the same class may be due to come changes so i did : "select2-hidden-accessible" if(element.hasClass('select2-hidden-accessible') && element.next('.select2-container').length) { error.insertAfter(element.next('.select2-container')); }Khajeh

© 2022 - 2024 — McMap. All rights reserved.