【问题标题】:Laravel 5.4: Unable to customise reset password validationLaravel 5.4:无法自定义重置密码验证
【发布时间】:2018-01-29 22:34:57
【问题描述】:

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

我已将以下方法从 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】:

    我认为问题出在验证上,因为您使用的是 $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'
    ]);
    

    【讨论】:

    • 您好,感谢您的帮助。我已经测试了上述内容,表单似乎仍然从我已删除的验证规则中返回错误,响应变量似乎是造成这种情况的原因。
    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 2020-07-21
    • 2017-12-09
    • 2017-08-04
    • 2019-07-04
    • 2019-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多