D
D
daashuun2020-11-24 10:22:39
JavaScript
daashuun, 2020-11-24 10:22:39

Why does the error message disappear?

I have a form where there are several inputs that are added dynamically from an ajax request, so the client-side validation set in the model does not work for them (I tried to find at least some information on whether it is possible to add validation when forming the input , but I didn’t find anything, if this is somehow solved - a separate huge thank you). Therefore, I wrote my own validator in jQuery, everything works as it should, it is validated, but the error message disappears (it disappears, appears for a second and disappears), although it does not skip data. But if all other fields are filled, then the message remains. And I just don’t understand why they disappear, it seems to me that I have tried, probably everything already.

$(document).ready(function(){

    $('input[name="NewForm[experience]"]').change(function(){
        var check = $(this).attr('id').slice(-1);
        addExp(check);
    });

    $('body').on('focusout', '.require', function() {
        var p = $(this).parent().children('p');
        if ($(this).val()=='') {
            $(this).closest('.form-group').addClass('has-error');
            $(this).attr('aria-invalid', true);
            p.text('Поле обязательно');
            p.addClass('help-block help-block-error ');
        } else {
            d = new Date();
            var invalid =  ($(this).attr('placeholder')=='2006') ? ($.isNumeric($(this).val())&&(parseInt($(this).val())>1899)&&(parseInt($(this).val())<=d.getFullYear())) : ($(this).val().search('\[А-Яа-яЁё]+$') == 0);
            if (invalid) {
        console.log(1);
                $(this).closest('.form-group').removeClass('has-error');
                $(this).attr('aria-invalid', false);
                p.text('');
                p.removeClass('help-block help-block-error ');
            } else {
                $(this).closest('.form-group').addClass('has-error');
                $(this).attr('aria-invalid', true);
                p.text('Поле заполнено неправильно');
                p.addClass('help-block help-block-error ');
            }
        }
    });

    $('#new').on('submit', function() {
        if ($('.require').length>0) {
            var thisInvalid = false;
            $.each($('.require'), function() {
                $(this).focusout();
                if ($(this).attr('aria-invalid')=='true') thisInvalid = true;
            });
            if (thisInvalid) return false;
        };
    });

});

function addExp (add) {
    if (add == 1) { 
        $child = $("#exp").children();
        id = ($child.length != 0) ? parseInt($child.last().attr('id').slice(-1))+1 : 1;
        $.ajax({
        type: "POST",
        url: "http://yii2/web/index.php?r=site%2Fexp",
        data: {
            'add' : add,
            'id' : id,
        },
        success: function (res) {
            $(res).appendTo('#exp') ;
            $('#add').addClass('is_visible');
        },
        error: function () {
            $('error').appendTo('#exp');
        }
    })} else {
        $('#exp').empty();
        $('#add').removeClass('is_visible');
    } 
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question