Answer the question
In order to leave comments, you need to log in
Yii2: how does unique work in server validation?
I exaggerate for convenience.
Let's say I have a Users.php model , there is nothing interesting in it, just a model, inherits AR, corresponds to a simple table with users: id , userName , userPassword .
There is a second model - Signup.php - respectively used to register a new user. It has a method:
public function rules()
{
return [
[['userName','userPassword'],'required'],
['userName','unique','targetClass'=>'app\models\Users'],
];
}
public function signup()
{
$user = new Users;
$user->userName = $this->userName;
$user->userPassword = $this->userPassword;
return $user->save();
}
public function actionSignup()
{
$model = new Signup();
if( \Yii::$app->request->post('Signup') )
{
$model->attributes = \Yii::$app->request->post('Signup');
if($model->validate() && $model->signup())
{
return $this->goHome();
}
}
return $this->render('signup',['model'=>$model]);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question