use enableAjaxValidation in ActiveForm

Regards

Try to validate the rules using ajax scenario. The idea is to activate the checkbox change the scenario, but I fail the validation rules are displayed (:[

This is the model class




 public function rules()

    {

        return [

             [['field_pk_fk', 'field1','field2'], 'required','on'=>'On JI'],

            [['field_pk_fk', 'field1','field2','field3','field4'], 'required','on'=>'Off JI']

}



This is the form




$form = ActiveForm::begin(['layout'=>'horizontal','id'=>'result-form',

    'enableAjaxValidation'=>true,

    'enableClientValidation'=>true]); ?>


 <?= $form->field($model, 'field1')->checkbox(array('value' => 'Yes', 'uncheck' => 'No')) ?>


    <?= $form->field($model, 'field2')->textarea(['maxlength' => 1000]) ?>


  <?= $form->field($model, 'field3')->textarea(['maxlength' => 1000]) ?>


  <?= $form->field($model, 'field4')->textarea(['maxlength' => 1000]) ?>




This is the controlling class


 public function actionCreate()

    {

        

         $model = new Resultado(); 

         $this->performAjaxValidation($model);      

        if($model->load(Yii::$app->request->post()))   

        {

       if($model->field1 === 'Yes')

       {

        $model->setScenario ('On JI');

       }

       else           

       {

         $model->setScenario ('Off JI'); 

       }

       }

       

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->field_pk_fk]);

        } else {

            return $this->render('create', [

                'model' => $model,

            ]);

        }

    }

    

    protected function performAjaxValidation($model)

    {

        if(isset($_POST['ajax']) && $_POST['ajax']==='result-form')

        {

         $model->validate();

          Yii::app()->end();

        }

    }

Hi!

Please try:




$form = ActiveForm::begin(['layout'=>'horizontal','id'=>'result-form',

    'enableAjaxValidation'=>true,

    'validationUrl' => ['mycontroller/ajax-validation'],

    'enableClientValidation'=>false

]); 

?>



And name your ajax validate function for example:




public function actionAjaxValidation(){

  // some other code here

  $mymodel = new MyModel; 

  $mymodel->load(Yii::$app->request->post());

  Yii::$app->respone->format = Response::FORMAT_JSON; 

  return ActiveForm::validate($mymodel);

}



I guess the problem is that the validation rules for clientValidation are still executed.

Regarding validation I made very much progress when I realized that there are actually 3 different validation methods:

[list=1]

[*]Classic Serverside validation in create method.

[*]Serverside Ajax validation.

[*]Pure Clientside validation.

[/list]

If you want to customize the pure client side validation take a look at:

http://www.yiiframework.com/doc-2.0/guide-input-validation.html

Read at "Client-Side Validation".

Best Regards