密码重置器验证器

时间:2019-03-07 15:51:38

标签: php laravel

我正在安装一个新的laravel应用程序,长话短说,我得到php artisan make:auth工作的重置密码功能

但是我需要将重置密码的最小长度更改为4,框架不允许我这样做。

到目前为止我的尝试

这是ResetPasswordController.php

    class ResetPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
     */

    use ResetsPasswords;

    /**
     * Where to redirect users after resetting their password.
     *
     * @var string
     */
    protected $redirectTo = '/dashboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function rules()
    {
        return [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:4',
        ];
    }
}

此功能有效,当我尝试提交3个字符时,错误正确显示。 The password must be at least 4 characters.

但是,如果我提交4个字符,则会显示此错误Passwords must be at least eight characters and match the confirmation. 看完laravel文档后 此功能/Illuminate/Auth/Passwords/PasswordBroker.php @validatePasswordWithDefaults是元凶。 link

这是一个错误吗?还是不知道如何使用重置密码功能的我? 感谢您的帮助

2 个答案:

答案 0 :(得分:0)

替代rules函数,而不是覆盖reset函数。 现在是我的,正在工作。

    public function reset(Request $request)
    {
        $request->validate([
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:4',
        ], $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.
        $this->broker()->validator(function ($credentials) {
            return mb_strlen($credentials['password']) >= 6;
        });
        $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($request, $response)
                    : $this->sendResetFailedResponse($request, $response);
    }

答案 1 :(得分:0)

Laravel 7 中的工作原理是在您的 ResetPasswordController.php

中添加以下代码
protected function rules()
    {
        return [
            'token' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed|min:4',
        ];
    }

注意:它会覆盖位于 ResetsPasswords.php 文件夹中的 vendor/laravel/ui/auth-backend规则功能