2025-06-18 00:26:39…
- User
2025-06-18 00:26:39 [::1][-][3s6j2ch00pqf5qqcp9vettc94n][error][yii\web\HttpException:400] yii\web\BadRequestHttpException: Missing required parameters: token in /app/vendor/yiisoft/yii2/web/Controller.php:173
Stack trace: #0 /app/vendor/yiisoft/yii2/base/InlineAction.php(54): yii\web\Controller->bindActionParams() #1 /app/vendor/yiisoft/yii2/base/Controller.php(184): yii\base\InlineAction->runWithParams() #2 /app/vendor/yiisoft/yii2/base/Module.php(555): yii\base\Controller->runAction() #3 /app/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction() #4 /app/vendor/yiisoft/yii2/base/Application.php(387): yii\web\Application->handleRequest() #5 /app/web/index.php(125): yii\base\Application->run() #6 {main} 2025-06-18 00:26:39 [::1][-][3s6j2ch00pqf5qqcp9vettc94n][error][app\controllers\ErrorController::logException] [ 'message' => 'Missing required parameters: token', 'code' => 0, 'stackTrace' => '#0 /app/vendor/yiisoft/yii2/base/InlineAction.php(54): yii\web\Controller->bindActionParams() #1 /app/vendor/yiisoft/yii2/base/Controller.php(184): yii\base\InlineAction->runWithParams() #2 /app/vendor/yiisoft/yii2/base/Module.php(555): yii\base\Controller->runAction() #3 /app/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction() #4 /app/vendor/yiisoft/yii2/base/Application.php(387): yii\web\Application->handleRequest() #5 /app/web/index.php(125): yii\base\Application->run() #6 {main}', 'context' => [ 'errorId' => 'error_685207bf0448b6.48013460', 'userId' => 'guest', 'requestUri' => '/index.php?r=site%2Freset-password', 'statusCode' => 400, 'exceptionClass' => 'yii\web\BadRequestHttpException', ], ]
I dont get it...
class SiteController extends SeoController { public function actionRequestPasswordReset(): Response|string { if (!$this->_isGuest) { return $this->goHome(); } // Setup $model. $model = new PasswordResetRequestForm(); $request = \Yii::$app->request; if (($request->isPost) && ($model->load($request->post()) && $model->validate())) { // Perform the post-validation action. if ($model->process()) { $msg = \Yii::t( 'app', '<h4 class="alert-heading">Password Reset Email Sent</h4> <p>Please check your email inbox for a password reset email. To complete the process, click the provided link, or copy and paste it into your browser.</p>' ); \Yii::$app->session->setFlash('success', $msg); return $this->goHome(); } // Verification failed. $msg = \Yii::t( 'app', '<h4 class="alert-heading">Unable to Reset Password</h4> <p>We apologize, but we are unable to reset the password for the provided email address.</p>' ); \Yii::$app->session->setFlash('error', $msg); } // No need for isGet check, default rendering will be for GET or any unhandled POST. return $this->render('request-password-reset-token', [ //'pageTitle' => $pageTitle, 'model' => $model, ]); } }form model
<?php declare(strict_types=1); namespace app\models\forms; use Yii; use yii\base\Model; use yii\base\Exception as YiiBaseException; use yii\db\Exception as YiiDbException; use app\models\User; use app\helpers\AppHelper; use app\helpers\ErrorMessageHelper; use app\helpers\ExceptionLogger; use app\helpers\LogHelper; use app\helpers\ParamHelper; use app\helpers\RoleHelper; class PasswordResetRequestForm extends Model { public string $email = ''; private string $_appName; private string $_emailSupport; public function __construct(array $config = []) { $this->_appName = AppHelper::name(); $this->_emailSupport = ParamHelper::get('email.support'); parent::__construct($config); } public function rules(): array { return [ ['email', 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'exist', 'targetClass' => User::class, 'filter' => function ($query) { // @var $query \app\models\UserQuery $query->active(); }, 'message' => ErrorMessageHelper::genericFormError(), //'message' => \Yii::t('app', 'There is no user with this email address.'), ], ]; } public function process(): bool { // @var $user User $user = User::find() ->where(['email' => $this->email]) ->active() ->notLocked() ->one(); if ($user === null) { return false; } $transaction = \Yii::$app->db->beginTransaction(); try { if (!User::isPasswordResetTokenValid($user->password_reset_token)) { $user->generatePasswordResetToken(); if (!$user->save(false, ['password_reset_token'])) { throw new YiiDbException("Failed to save user data to the database."); } } // If all saves are successful, commit the transaction. $transaction->commit(); $isSent = \Yii::$app->mailer->compose( ['html' => 'emailPasswordResetToken-html', 'text' => 'emailPasswordResetToken-text'], [ 'emailAppName' => $this->_appName, 'emailSupport' => $this->_emailSupport, 'emailUser' => $user, ], ) ->setFrom([$this->_emailSupport => \Yii::t( 'appEmail', '{appName} robot', ['appName' => $this->_appName]) ]) ->setTo($user->email) ->setSubject(\Yii::t( 'appEmail', 'Password reset for {appName}', ['appName' => $this->_appName]) ) ->send(); if (!$isSent) { throw new YiiBaseException("The email could not be sent to {$recipientEmail}."); } return true; } catch (\Throwable $e) { // An error occurred; roll back the transaction. $transaction->rollBack(); // Use the ExceptionLogger to handle and log the exception. ExceptionLogger::log($e, 'password_reset_error_'); } return false; } } - Assistant
- User
$behaviors['verbs'] = [ 'class' => VerbFilter::class, 'actions' => [ 'login' => ['get', 'post'], 'logout' => ['post'], 'request-password-reset' => ['get', 'post'], 'reset-password' => ['get', 'post'], 'resend-verification-email' => ['get', 'post'], 'verify-email' => ['get'], 'verify-delete-account' => ['get'], ], ];
- Assistant
- User
so how would you refactor actionResetPassword? send complete code.
- Assistant
- User
so i would need to refactor the ResetPasswordForm aswell than
- Assistant
Not necessarily—you can keep your
ResetPasswordFormclass as is, since it takes the token in its constructor and validates it there.The refactor I suggested for
actionResetPasswordensures that the token is always provided when instantiatingResetPasswordForm.
Here's why you do not need to refactor
ResetPasswordForm: - User
but in the ResetPasswordForm do i need to set "public string $token = '';" ??
- Assistant
No, you don’t need to add a public
$tokenproperty to yourResetPasswordFormbecause:- The token is passed via the constructor and stored internally (in your code as a private property or just used to find the user).
- User
that fixed it
- Assistant
Great to hear that fixed it! 🎉
Just to recap, the key points were:
- Make the
tokenparameter optional in your action and retrieve it from GET or POST. - Always pass the token to your form model constructor.
- Keep the token as a constructor parameter only—no need to add it as a public form attribute.
- Ensure your form submits include the token in URL or as hidden input.
If you run into any other Yii2 issues or need help polishing this flow further, feel free to ask. Happy coding! 🚀
- Make the