Laravel 5.4:无法自定义重置密码验证

问题描述 投票:0回答:1

如何在Laravel的auth脚手架中覆盖重置密码的默认验证?

我已将ResetsPasswords特征中的以下方法复制到我的ResetPasswordController中:

public function reset(Request $request)
{
    $this->validate($request, $this->rules(), $this->validationErrorMessages());

    // Here we will attempt to reset the user's password. If it is successful we
    // will update the password on an actual user model and persist it to the
    // database. Otherwise we will parse the error and return the response.
    $response = $this->broker()->reset(
        $this->credentials($request), function ($user, $password) {
            $this->resetPassword($user, $password);
        }
    );

    // If the password was successfully reset, we will redirect the user back to
    // the application's home authenticated view. If there is an error we can
    // redirect them back to where they came from with their error message.
    return $response == Password::PASSWORD_RESET
                ? $this->sendResetResponse($response)
                : $this->sendResetFailedResponse($request, $response);
}

/**
 * Get the password reset validation rules.
 *
 * @return array
 */
protected function rules()
{
    return [
        'token' => 'required',
        'email' => 'required|email',
        'password' => 'required|confirmed|min:6',
    ];
}

如果我删除规则,例如使用长度小于6个字符的密码时,仍会返回“min:6”错误消息。

我已经提到了相关的文档,但是我无法从中获取它所需的内容。

任何帮助将非常感谢,提前感谢。

php laravel laravel-5.4
1个回答
1
投票

我认为问题来自验证,因为您使用的是$this->validate方法,您无法完全控制验证过程。

通常,覆盖ResetPasswordController中的验证规则可以解决问题。

这是另一种方式,让我们直接使用Validator类,这样我们就可以完全控制Auth\ResetPasswordController

public function reset(Request $request)
{
    $validator = validator($request->all(), [
        'token' => 'required',
        'email' => 'required|email',
        'password' => 'required|confirmed|min:6',
    ], $this->validationErrorMessages());

    if($validator->fails())
    {
        //do stuffs here like
        return redirect()->back()->withErrors($validator);
    }

    //if we get here means validation passed :) so lets allow these to continue

    // Here we will attempt to reset the user's password. If it is successful we
    // will update the password on an actual user model and persist it to the
    // database. Otherwise we will parse the error and return the response.
    $response = $this->broker()->reset(
        $this->credentials($request), function ($user, $password) {
        $this->resetPassword($user, $password);
    }
    );

    // If the password was successfully reset, we will redirect the user back to
    // the application's home authenticated view. If there is an error we can
    // redirect them back to where they came from with their error message.
    if($response == Password::PASSWORD_RESET)
    {
        //means password reset was successful
        return redirect()->to('/dashboard');
    }else{
        //means reset failed
        return redirect()->back()
            ->withInput($request->only('email'))
            ->withErrors(['email' => trans($response)]);
    }
}

注意:确保使用正确的路线您可以使用:

Route::post('/reset-password', [
    'uses'=>'Auth\ResetPasswordController@reset',
    'as' => 'reset'
]);
© www.soinside.com 2019 - 2024. All rights reserved.