Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch on\off frontend form validation for some fields in yii2?

I have got difficult form in yii2 view, where some fields show or hide. It decide from user field choises, select options in the form. I write this frontend logic with custom jquery file. All is ok. But when I submit form - hidden fields stay without validation and nothing is happend.How I can kill ofrontend validation, when fields are hiiden and switch on it, when fields are visible?

like image 858
Сергей Илларионов Avatar asked Mar 04 '15 14:03

Сергей Илларионов


3 Answers

$form->field($model, 'youAttribute', ['enableClientValidation' => false])->textInput();

The ActiveField class has a property enableClientValidation, you can simply set this property to false if you want to disable clientValidation form some fields.

like image 88
Max Wen Avatar answered Sep 28 '22 21:09

Max Wen


To disable client side validation. Begin your active form like this.

ActiveForm::begin(['enableClientValidation'=>false]);
like image 33
Rasikh Mashhadi Avatar answered Sep 28 '22 23:09

Rasikh Mashhadi


To remove a field from validation:

$('#yourFormID').yiiActiveForm('remove', 'yourinputID');

To add a field to validation list:

$('#yourFormID').yiiActiveForm('add', {
id: 'country',
        name: 'yourinputID',
        container: '.field-inputID', //or your cllass container
        input: '#yourinputID',
        error: '.help-block',  //or your class error
        validate:  function (attribute, value, messages, deferred, $form) {
            yii.validation.required(value, messages, {message: "Validation Message Here"});
        }
    }); 

And don't forget conditional validation in your model. More info

like image 40
hesselek Avatar answered Sep 28 '22 21:09

hesselek