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

时间:2017-08-21 19:11:16

标签: php laravel laravel-5.4

如何在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'使用长度小于6个字符的密码时,仍会返回错误消息。

我已经参考了相关文档,但我还没有能够从中获取所需的文档。

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

1 个答案:

答案 0 :(得分: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'
]);