Toma Tomov
Toma Tomov

Reputation: 1654

Yii2 client side validation refreshes the page on form submit

I am trying to perform client side check but the page is being refreshed. My rules looks like:

public function rules()
    {
        return [
            [['email'], 'required'],
            [['email','cron'], 'string'],
            [['email'], 'string', 'max' => 100],
            [['cron', 'group', 'type'], 'integer'],
            [['email'], 'unique'],
            ['group', 'required', 'when' => function($model){
                return $model->type == 1;
            }, 'whenClient' => 'function(attribute, value){
                return 1==2;
            }']
        ];
    }

The form enableClientValidation is switched to true like this :

<?php $form = ActiveForm::begin([
                    'enableClientValidation' => true
                ]) ?>

Where is my mistake ? Thank you in advance!

Upvotes: 0

Views: 363

Answers (2)

Insane Skull
Insane Skull

Reputation: 9358

You need to set enableAjaxValidation property of AcitveForm:

$form = ActiveForm::begin([
   'id' => 'form-id',
   'enableAjaxValidation' => true,
]);

Controller

if ($model->load(Yii::$app->request->post())) {
    if(Yii::$app->request->isAjax) {
        return $this->asJson(ActiveForm::validate($model));
    }
}

Yii2 Ajax Validation

asJson()

Upvotes: 1

Sersh
Sersh

Reputation: 308

<?php $form = ActiveForm::begin([
                    'id' => 'form',
                    'enableAjaxValidation' => true
                ]) ?>

Upvotes: 1

Related Questions