0

Very weird behavior i found out with yii2 ajax validation i found today.

Basically i was trying to perform ajax validation on 1 field of the form to check that the input is unique and not found anywhere in that field of that table.

If you ask me the following code should work correctly:

In model:

public function scenarios(){
   return ['update' => ['username']];
}
public function rules(){
   return [['username'], 'unique'];
}

In Controller:

public function actionUpdate(){
    $user = User::findmodel.. bla bla
    some more bla bla..

    if(Yii::$app->request->isAjax){

        $user->setScenario('update');

        $user_post_ajax = Yii::$app->request->post('User');
        $user->username = $user_post_ajax['username'];
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return ActiveForm::validate($user);
    }

    some more bla bla bla..
}

In View:

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($user, 'username', ['enableAjaxValidation' => true])->textInput() ?>
    <?= Html::submitButton('Save', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    <?php ActiveForm::end(); ?>

This should work, but it doesn't, it doesn't display the validation errors, in this case something like: 'Username is already taken', it doesn't display neither valid nor invalid. It performs the ajax request, and i can see the json response in the dev tools but it doesn't display at all. But, When you move the $user->setScenario('update'); outside the if condition, above the if(Yii::$app->request->isAjax) only then it works perfectly fine. I don't understand this, am i missing something? Why using setScenario when ajax request changes the behavior?

2
  • I have little same problem Commented Apr 18, 2017 at 8:39
  • I have just change form id and it's work for me Commented Apr 18, 2017 at 9:17

1 Answer 1

0

You're overriding the scenarios() function from the Yii Model, thus not validating any attributes.

Check: https://github.com/yiisoft/yii2/blob/master/framework/base/Model.php#L184

What you should do, is:

public function rules(){
  return [['username'], 'unique', 'on'=>['update']];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thnx for your answer, but that is not the problem, i dont want username unique only on 1 scenario. As i said the problem stands when i put the $user->setScenario('update'); inside the if statement to check if it is ajax request it breaks and doesn't display the error in frontend. When i put that above the if check, it works just fine and it displays the error. In both cases the $user->setScenario('update'); is executed, but when it is inside the if statement it brings that problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.