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

后端 未结 6 2275
灰色年华
灰色年华 2021-02-20 01:41

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 jq

6条回答
  •  南旧
    南旧 (楼主)
    2021-02-20 02:02

    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

提交回复
热议问题