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();
}
}