gpt4 book ai didi

php - laravel 5.1 Password::reset 返回 passwords.password

转载 作者:可可西里 更新时间:2023-10-31 22:41:36 25 4
gpt4 key购买 nike

我在 Controller 中有此功能,但我无法重置密码,因为我想将字符长度更改为 5 位数字。

public function postReset(Request $request)
{
$this->validate($request, [
'token' => 'required',
'password' => 'required|confirmed|digits:5',
]);

$credentials = $request->only(
'email', 'password', 'password_confirmation', 'token'
);

$response = Password::reset($credentials, function ($user, $password) {
$this->resetPassword($user, $password);
});

dd($response);
switch ($response) {
case Password::PASSWORD_RESET:
return redirect($this->redirectPath());

default:
return redirect()->back()
->withInput($request->only('email'))
->withErrors(['email' => trans($response)]);
}
}

protected function resetPassword($user, $password)
{
$user->password = bcrypt($password);
$user->save();
Auth::login($user);
}

但它总是说:

Whoops! There were some problems with your input.

密码必须至少包含六个字符并与确认匹配。

当我添加时:

dd($response);

它打印:

passwords.password

知道如何解决这个问题吗?

最佳答案

发生这种情况是因为 Illuminate\Auth\Passwords\PasswordBroker 中存在硬编码验证.

reset方法被调用时,它总是会调用validateReset首先,依次调用 validateNewPassword :

public function validateNewPassword(array $credentials)
{
list($password, $confirm) = [
$credentials['password'],
$credentials['password_confirmation'],
];

if (isset($this->passwordValidator)) {
return call_user_func(
$this->passwordValidator, $credentials) && $password === $confirm;
}

return $this->validatePasswordWithDefaults($credentials);
}

默认情况下,passwordValidator 未设置。所以validatePasswordWithDefaults将要求密码长度至少为 6 个字符。

您可以使用 Password::validator 设置一个 passwordValidator ,它接受一个闭包,该闭包必须返回一个 bool 值,指示给定的凭据是否有效。这需要在 Password::reset 之前完成。

例如,将验证器更改为要求密码长度恰好为 5 个字符将特别满足您的要求。

Password::validator(function($credentials)
{
return strlen($credentials['password']) === 5;
});

关于php - laravel 5.1 Password::reset 返回 passwords.password,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33577760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com