Answer the question
In order to leave comments, you need to log in
Why is my form redirecting to renderAjax?
I have a password recovery form with phone verification, form model new PasswordResetRequestForm
.
And when the user has confirmed the phone, then I switch to another form model. The form is normally displayed new ResetPasswordForm.
I only register other way in the form action. When I make validation errors, it redirects me to another page. Where should I look for the error?
My implementation in a nutshell
public function actionRequestPasswordReset()
{
$scenario = PasswordResetRequestForm::SCENARIO_EMAIL;//Делаем сценарий по умолчанию по email
$request = Yii::$app->request->post('PasswordResetRequestForm');
//Для каждой проверки свои сценарии прописываем
if (!Yii::$app->session->get('phone') && $request['phone'] !== '') {
$scenario = PasswordResetRequestForm::SCENARIO_PHONE;
} else if (Yii::$app->session->get('phone') && $request['code'] !== '') {
$scenario = PasswordResetRequestForm::SCENARIO_CODE;
}
//Для подтверждение пароля
$resetPassword = new PasswordResetRequestForm(['scenario' => $scenario]);
if ($resetPassword->load(Yii::$app->request->post()) && $resetPassword->validate()) {
//Для каждого сценарии свои шаги по восстановление пароля
if ($scenario === PasswordResetRequestForm::SCENARIO_EMAIL) {
,,,
} elseif ($scenario === PasswordResetRequestForm::SCENARIO_CODE) {
$token = $resetPassword->tokenFormation(Yii::$app->session->get('phone'));//Формируем токен
//Если токен сформировался нормально, то формируем форму для изменение пароля
$model = new ResetPasswordForm($token);
return $this->renderAjax('requestPasswordResetToken', [
'model' => $model,
'success' => false,
'token' => $token
]);
}
}
return $this->renderAjax('requestPasswordResetToken', [
'resetPassword' => $resetPassword,
'success' => false,
]);
}
/**
* @brief Страница для формирование подтверждение пароля
*
* @param $token
*
* @return string
*/
public function actionResetPasswordPhone($token)
{
$model = new ResetPasswordForm($token);
$model->scenario = ResetPasswordForm::SCENARIO_PHONE;
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
Yii::$app->session->setFlash('success', 'Новый пароль успешно установлен');
return $this->redirect(['site/login']);
} else {
return $this->renderAjax('requestPasswordResetToken', [
'model' => $model,
'success' => false,
'token' => $token
]);
}
}
<?php if (isset($model) && isset($token)): ?>
<?php $form = ActiveForm::begin([
'action' => Url::to(['site/reset-password-phone', 'token' => $token]),
'id' => 'form-password-reset',
'options' => ['class' => 'f_regular'],
'fieldConfig' => [
'options' => [
'class' => 'form_field'
],
'template' => "{label}\n{input}\n{hint}",
],
]); ?>
<div class="form_field password_reset">
<div class="form_field_rs">
<input
name="ResetPasswordForm[password]"
type="password"
id="reset_pass_password"
class="input <?= $model->getFirstError('password') ? '__error' : '' ?>"
placeholder="Пароль"
value="<?= $model->password ?>"
>
<div class="err_field __visible"><?= $model->getFirstError('password') ?></div>
</div>
</div>
<div class="form_field confirm_reset">
<div class="form_field_rs">
<input
name="ResetPasswordForm[confirm]"
type="password"
id="reset_pass_confirm"
class="input <?= $model->getFirstError('confirm') ? '__error' : '' ?>"
placeholder="Подтвердите пароль"
value="<?= $model->confirm ?>"
>
<div class="err_field __visible"><?= $model->getFirstError('confirm') ?></div>
</div>
</div>
<input type="submit" id="reset_pass_btn" class="f_heavy btn" value="Восстановить">
<?php ActiveForm::end(); ?>
<?php endif; ?>
/site/reset-password-phone?token=RHIekUD7idfndMNcs1wuh2SIWrjvJAOd_1552285868
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