Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

select2 and jquery validation not working properly

Trying to use validation on select2 with a few issues :

  • the error message will show, but it will not remove when a valid entry is entered.

  • I am loading an initial value which works fine... the validator, however, does not recognize the value and tells me it is invalid... I have to manually type in the same value and then it validates, but still does not remove the error class/message showing it is valid.

form :

<div class="col-md-12 margin-bottom-30 form-group">
    <div class="input-modal-group">
        <label for="vedit-filter" class="f-14"><b>filter :</b></label>
        <input id="vedit-filter" type="text" name="settings[filter]" class="form-control select2"/>
    </div>
</div>
<input id="filter_default" type="hidden" name="settings[original]" value="<?php echo escapeStr($result[filter]); ?>"/>

js :

// get the default filter           
var default_filter = $("#filter_default").val();

$("#vedit-filter").select2({
    //placeholder: "Select or enter...",
    allowClear: true,
    multiple: false,
    ajax: {
        dataType: 'json',
        url: '/process/get_filter_list.php',
        results: function (data) {
            return {results: data};
        }
    },
    createSearchChoice:function(term, data) {
        if ($(data).filter(function() {
            return this.text.localeCompare(term)===0; }).length===0) {
                return {id:term, text:term};
            }
        },
    initSelection : function (element, callback) {
        var obj= {id:default_filter, text:default_filter};
        callback(obj);
    }
});

$('#filters-edit').validate({
    errorElement: 'span', //default input error message container
    errorClass: 'help-block', // default input error message class
    focusInvalid: false, // do not focus the last invalid input
    rules: {
        'settings[filter]': {
            required: true
        }
    },

    messages: {
        'settings[filter]': {
            required: "Filter is required."
        }
    },

    highlight: function (element) { // hightlight error inputs
        $(element)
            .closest('.form-group').addClass('has-error');
    },

    unhighlight: function (element) { // un-hightlight error inputs
        $(element)
            .closest('.form-group').removeClass('has-error'); 
    },

    errorPlacement: function (error, element) {
        error.insertAfter(element.closest('.input-modal-group'));
    },

    // ajax submit
    submitHandler: function (form) {

    ...submit stuff below
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By default, the validation on a field is only triggered by these events (for a type="text" field)...

  • onfocusout, when the user leaves a field (validates just the field)

  • onkeyup, when the user is typing within a field (validates just the field)

  • onclick, when the user clicks the submit button (validates the whole form)

If you need to programmatically trigger validation after you programmatically change the value of a field, then you need to invoke the .valid() method on that field. It will effectively trigger validation similar as the events above.

$('#myField').valid();  // validates just field element with id="myField"

$('#myForm').valid();  // validates the whole form

So you'll need to find a method within select2 that is fired whenever the value is changed and put .valid() inside of that. It looks like the change event is what you'll need.

$("#vedit-filter").select2({
    // your options here
}).on('change', function() {
    $(this).valid();
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...