I
I
Ivan Ivanov2020-12-11 21:15:52
JavaScript
Ivan Ivanov, 2020-12-11 21:15:52

How to force validate form data in Yii2?

There is a drop-down step-by-step filling in the form, at the end there is a submit data button. You need to do this: when you press the button, first validate the data (which happens by itself), then, if an error is detected, open the desired tab. The problem is that if you immediately catch the button click event, then there will be no errors at the time of checking, js code:

$('.send-btn').click(function(e) {
            $('#step-content-1').find('.help-block-error').each(function() {
                if(needStep == 0) {
                    if($(this).text() != '') {
                        needStep = 1;
                    }
                }
            });
            $('#step-content-2').find('.help-block-error').each(function() {
                if(needStep == 0) {
                    if($(this).text() != '') {
                        needStep = 2;
                    }
                }
            });  

            if(needStep > 0) {
                closeAll();
                toggleStep(needStep);
            }
        });

All this works from the second click, it needs to work from the first.
Tried like this, didn't work
$("#myForm").data("yiiActiveForm").submitting = true;
$("#myForm").yiiActiveForm("validate");

I also tried to force click a second time on the first click (via .click() - didn't help)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2020-12-11
@KingstonKMS

Form validation must take place on the server.
Check out https://github.com/samdark/yii2-cookbook/blob/mast...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question