我正在安装一个新的 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',
];
}
}
Run Code Online (Sandbox Code Playgroud)
此功能有效,当我尝试提交 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是罪魁祸首。关联
这是一个错误吗?或者只是我不知道如何使用重置密码功能?谢谢您的帮助
不要覆盖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);
}
Run Code Online (Sandbox Code Playgroud)