How to remove the client validation for the fields

2019-07-23 16:47发布

问题:

Here my scenario is if the check box is uncheck then the corresponding fields will be disabled then for the disabled fields the client validation should not perform here for example if i uncheck the checkbox of (has a father) then the fields related to that check box like father first name, last name... so for these fields client validation should not perform . If i again check that checkbox then the client validation should perform

Here my problem is when i am uncheck the checkbox also the client side validation is happening and the form is not submiting can any one help me in this scenario. Here is my sample code

<?php echo $form->checkBox($modelFamilyFather, '[1]type',   array('id'=>'type', 'checked'=>'checked','onChange' => "
    if(this.value==checked)
    {

         $('#" . CHtml::activeId($modelFamilyFather, '[1]firstname') .
    "').removeAttr('disabled','disabled');

            $('#" . CHtml::activeId($modelFamilyFather,
    '[1]lastName') . "').removeAttr('disabled','disabled');



         $('#" . CHtml::activeId($modelFamilyFather, '[1]occupation') .
    "').removeAttr('disabled','disabled');

    $('#" . CHtml::activeId($modelFamilyFather, '[1]averageMonthlyIncome') .
    "').removeAttr('disabled','disabled');

    }

    else {
        $('#" . CHtml::activeId($modelFamilyFather, '[1]firstname') .
    "').attr('disabled','disabled');

            $('#" . CHtml::activeId($modelFamilyFather,
    '[1]lastName') . "').attr('disabled','disabled');



         $('#" . CHtml::activeId($modelFamilyFather, '[1]occupation') .
    "').attr('disabled','disabled');

    $('#" . CHtml::activeId($modelFamilyFather, '[1]averageMonthlyIncome') .
    "').attr('disabled','disabled');

    }



    "))." <label for='Father'>Has a Father</label>";

    ?>

回答1:

You could use scenarios to do this.

Change your validation rules for $modelFamilyFather to include 'on' => 'hasFather'. Example:

array('firstname, lastName, occupation, averageMonthlyIncome', 'required', 'on' => 'hasFather'),

That activates the rule for the "hasFather" scenario only.

Then edit your controller's performAjaxValidation method to change the scenario when the value of the checkbox changes:

I don't know the name of your form, checkbox (is it called "type"?), or model so you will need to replace "your-form-name" and "checkBoxName" below.

protected function performAjaxValidation($model)
{
    if(isset($_POST['ajax']) && $_POST['ajax']==='your-form-name')
    {
        if($_POST['ModelFamilyFather']['checkBoxName'] == 1){
            $model->scenario = 'hasFather';
        }
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
}

Then you will also need to edit the method that takes the input from the form. Again, I don't know the names or what that part of your code looks like but you need to do something like this:

if(isset($_POST['ModelFamilyFather']))
{
    $model->attributes=$_POST['ModelFamilyFather'];
    if($model->checkBoxName == 1)
        $model->scenario = 'hasFather';
    if($model->save())
        $this->redirect(array('view','id'=>$model->id));
}

Replace "ModelFamilyFather" with your model's name, and checkboxName with your checkbox name.

Three other things you need to check:

  1. Make sure that you have an attribute for the checkbox in your model. If you don't, you need to make one.
  2. Set the checkbox's default value to 0. So in your controller, before you send the model to the form, add $model->checkBoxName = 0;
  3. Make sure that your form has 'enableAjaxValidation'=>true, 'enableClientValidation' => true,

I have tested this locally with my own model, attributes, and checkbox and it worked.



标签: php yii