Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trigger active form validation manually before submit

Tags:

yii2

is it possible to call the active form validation programmatically via javascript? I need to call the validation procedure before doing some ajax operations.

like image 961
AleCat83 Avatar asked Feb 19 '15 15:02

AleCat83


People also ask

How do I validate a trigger form?

Set a onclick listener on the validate button to set a global flag(say justValidate ) to indicate this click is intended to check the validation of the form. And set a onclick listener on the submit button to set the justValidate flag to false.

Which method will force the form to be submitted after validation?

Submit 's default action is to submit the data, but if you give onSubmit a value of return false , it will not be submitted; just like how we can stop a link from being followed. Of course, if there are no problems, the function call will be replaced by true and the data will be submitted. Simple...


3 Answers

Guess I'm a bit late with a reply here but I just had the same question and the solution by soju did not work for me either.

So I looked a bit deeper into the JS-code of ActiveForm and found that it appears to monitor the status of each field in a variable and if the field is "untouched" the validation isn't triggered, unless submitting the actual form. So I changed my call to this:

var $form = $("#my-form"), 
    data = $form.data("yiiActiveForm");
$.each(data.attributes, function() {
    this.status = 3;
});
$form.yiiActiveForm("validate");

This now appears to be working as I expect.

like image 61
BlueZed Avatar answered Oct 12 '22 08:10

BlueZed


We can achieve that by merging @BlueZed and @S Rana's answer.

You can write below script, so we can check that if form has any error in it then form will not submit (Even It will work for tabular (wizards) like form ).

    var $form = $("#form"), 
        data = $form.data("yiiActiveForm");
    $.each(data.attributes, function() {
        this.status = 3;
    });
    $form.yiiActiveForm("validate");

    if ($("#form").find(".has-error").length) {
        return false;
    }
like image 7
DS9 Avatar answered Oct 12 '22 06:10

DS9


Thanks blue zed, but before this - to append form field, u need to do this stuff...

// your input

$inputData = $form->field($model,"prductName");

// this remove next line & convert double quotes to single quotes

$newInputData= trim(preg_replace('/\s+/', ' ',str_replace('"',"'", $inputData)));

// then append like this

$("#table").append("'.$newInputData.'");

// this worked for me along with blue zend solution like this below

$this->registerJs('
    $(document).on("click","input[type=text]",function(){
            var $form = $("#w0"), 
            data = $form.data("yiiActiveForm");
            $.each(data.attributes, function() {
                this.status = 3;
            });
            $form.yiiActiveForm("validate");        
    });
');
like image 2
Nikhil Kadam Avatar answered Oct 12 '22 07:10

Nikhil Kadam