How to enable / disable interface form validation for some fields in yii2?

I have a complex form in the yii2 view where some fields are shown or hidden. He selects the user field from the selection, selects the parameters in the form. I am writing this frontend logic with a custom jquery file. Everything is good. But when I submit the form, the hidden fields are left without verification, and nothing happens. How can I kill inrontend validation when fields are hiiden and enable it when fields are visible?

+9
source share
6 answers

To disable client-side validation. Start your active form as follows.

ActiveForm::begin(['enableClientValidation'=>false]); 
+15
source
 $form->field($model, 'youAttribute', ['enableClientValidation' => false])->textInput(); 

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

+19
source

You can set your active field using this code: (not active record , activefield sure)

 $activeField = $form->field($model, 'someField'); $activeField->enableClientValidation=false; $activeField ->enableAjaxValidation=false; 
+10
source

You can try to set default values ​​for attributes that are not set:

 [ // set "username" and "email" as null if they are empty [['username', 'email'], 'default'], // set "level" to be 1 if it is empty ['level', 'default', 'value' => 1], ] 

more details here

You can also use client-side conditional validation with the "whenClient" parameter when defining validators:

From the manual:

If you also need to support client-side conditional validation, you must configure the whenClient property, which accepts a string representing a JavaScript function whose return value determines whether or not to apply the rule. For instance,

 [ ['state', 'required', 'when' => function ($model) { return $model->country == 'USA'; }, 'whenClient' => "function (attribute, value) { return $('#country').val() == 'USA'; }"], ] 
+8
source

To remove a field from validation:

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

To add a field to the 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 the conditional check in your model. Additional Information

+1
source

For your form, use whenClient:

 ['name', 'required', 'when' => {serverSide Condition), 'whenClient' => "ut_utils.isAttributeVisible", ], ['name', 'string', 'min' => 2, 'max' => 28], ['name', 'trim'], 

And in ut_utils (JS):

 /** * Useful for FE validation (whenClient) to validate only if visible (ie valid input) * * @param attribute Obj containing all sorts of info about attr including container name :-) * @param value */ isAttributeVisible: function (attribute, value) { return $(attribute.container).is(':visible'); }, 

You will need to add β€œwhen” to test the server side, you can also add specific logic here or use a script to exclude attributes from the check ...

0
source

Source: https://habr.com/ru/post/1214671/


All Articles