Answer the question
In order to leave comments, you need to log in
Yii2 user registration. Why doesn't save() work?
SiteController
public function actionSignup()
{
$model = new User();
if ($model->load(Yii::$app->request->post())) {
if ($model->validate()) {
$model->save();
Yii::$app->session->setFlash('signUpOk');
return $this->refresh();
}
}
return $this->render('signup', [
'model' => $model,
]);
}
class User extends ActiveRecord implements IdentityInterface
Answer the question
In order to leave comments, you need to log in
You have the wrong model. You need to create one more SignupForm model (extends yii\base\Model), and in it the fields are already validated and the User'a instance is saved to the database. See how it's done in the advanced template (registration in frontend, user model in common).
Something is preventing the data from being saved, perhaps the beforeSave handler in User.
Flash message, in theory, should be like this:
If the validation has already been done, there is no point in doing it again in save():
Therefore, you need to save it like this:
if ($model->validate()) {
$model->save(false);
//..
}
if ($model->save()) {
//..
}
Okay, I understand the advantage. Now I'll try to add ... something :)
Thank you!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question