修复错误显示的验证错误时恢复的表单内容

This commit is contained in:
Chenx221 2024-02-10 12:02:29 +08:00
parent 068af76756
commit dcdecdad7d

View File

@ -156,7 +156,6 @@ class UserController extends Controller
Yii::$app->session->setFlash('error', 'Invalid username or password.'); Yii::$app->session->setFlash('error', 'Invalid username or password.');
} }
} }
return $this->render('login', [ return $this->render('login', [
'model' => $model, 'model' => $model,
]); ]);
@ -182,14 +181,15 @@ class UserController extends Controller
public function actionRegister() public function actionRegister()
{ {
$model = new User(['scenario' => 'register']); $model = new User(['scenario' => 'register']);
if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->password = Yii::$app->security->generatePasswordHash($model->password); $raw_password = $model->password;
$model->password = Yii::$app->security->generatePasswordHash($raw_password);
$model->auth_key = Yii::$app->security->generateRandomString(); $model->auth_key = Yii::$app->security->generateRandomString();
if ($model->save()) { if ($model->save(false)) { // save without validation
Yii::$app->session->setFlash('success', 'Registration successful. You can now log in.'); Yii::$app->session->setFlash('success', 'Registration successful. You can now log in.');
return $this->redirect(['login']); return $this->redirect(['login']);
} else { } else {
$model->password = $raw_password;
Yii::$app->session->setFlash('error', 'Failed to register user.'); Yii::$app->session->setFlash('error', 'Failed to register user.');
} }
} }