yii2: unique validator is not working

model public function rules() { return [ [[‘user_id’, ‘status’, ‘is_primary’,‘is_delete’], ‘integer’], [[‘title’], ‘string’, ‘max’ => 255], [[‘title’], ‘unique’], ]; } controller public function actionCreate() { $model = new EmailUser();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

view in view this only field i have <?= $form->field($model, 'title')->textInput(['maxlength' => 255]) ?>

what is issue ,i did not find?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 1
  • Comments: 24 (10 by maintainers)

Most upvoted comments

thnku ,i got but i did nt get it was in if condition so if it is not saving then it should go to else and form should stay there if error occurs

@ajkosh Now you are not checking save results… try this:

public function actionCreate(){
    $model = new EmailUser();
    if ($model->load(Yii::$app->request->post())) {
        $model->user_id=Yii::$app->user->identity->id;
        $model->email_otp=Yii::$app->getSecurity()->generateRandomString(8);
        if ($model->save()) {
            return $this->redirect(array('useremail/index'));
        }
    }
    return $this->render('create', [
        'model' => $model,
    ]);
}