我正在尝试重设密码,但如果密码长度小于6
,则无法重设密码。我正在验证用min:4
验证提交的密码,但是当我输入更多的4个字符表单时没有提交,但是当我尝试使用6个以上的表单时,它正在运行。
任何想法我的代码中出了什么问题。
<div class="reset_password_container">
<div class="reset_bg">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/password/reset') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="token" value="{{ $token }}">
<div class="find_account_container">
<div class="find_inner_logo">
<h5>{{ trans('messages.reset_password_form.reset_password') }}</h5>
</div>
<div class="find_form_dv">
<div class="reset_para_dv">
<p>{{ trans('messages.reset_password_form.text_1') }}</p>
<div class="reset_email_dv">
<p>{{ trans('messages.reset_password_form.email') }} <a href="javascript:void(0);">{{ $email }}</a></p>
</div>
</div>
<div class="reset_form_dv">
<input type="hidden" class="txt" name="ID" value="{{ $email }}">
<input type="password" class="txt" name="password" value="{{ old('password') }}" placeholder="{{ trans('messages.reset_password_form.password') }}">
<p class="error"></p>
<input type="password" class="txt" name="password_confirmation" value="{{ old('password_confirmation') }}" placeholder="{{ trans('messages.reset_password_form.password_confirmation') }}">
<p class="error">
@if ($errors->has('password'))
{{ $errors->first('password') }}
@endif
</p>
</div>
</div>
</div>
<div class="reset_footer_bg">
<div class="rest_btn_bg">
<button type="submit" class="btn btn-primary">{{ trans('messages.reset_password_form.confirm') }}</button>
</div>
</div>
</form>
</div>
</div>
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Support\Facades\Input;
use Mail;
use DB;
class PasswordController 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;
/**
* Create a new password controller instance.
*
* @return void
*/
public function __construct()
{
//$this->middleware('guest');
}
/**
* Get the post register / login redirect path.
*
* @return string
*/
public function redirectPath()
{
if (property_exists($this, 'redirectPath')) {
return $this->redirectPath;
}
return property_exists($this, 'redirectTo') ? $this->redirectTo : '/dashboard';
}
/**
* Display the password reset view for the given token.
*
* @param string $token
* @return \Illuminate\Http\Response
*/
public function getReset($token = null, Request $request)
{
if (is_null($token)) {
throw new NotFoundHttpException;
} else {
$userReset = DB::table('password_resets')->select('email')->where('token','=',$token)->get();
}
if (!empty($userReset)) {
return view('auth.reset')->with('token', $token)->with('email', $userReset[0]->email);
} else {
return redirect('/');
}
}
/**
* Send a reset link to the given user.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postEmail(Request $request)
{
//Input::get('ID') die;
$this->validate($request, ['ID' => 'required|email']);
// Pass data to reset password mail template
view()->composer('emails.password', function($view) {
$view->with([
'Nickname' => Input::get('nickname'),
]);
});
$response = Password::sendResetLink($request->only('ID'), function (Message $message) {
$message->subject('비밀번호 재설정 안내');
//$message->subject($this->getEmailSubject());
});
if ($response == "passwords.sent") {
$html = '<div class="img_left_dv"><img src="resources/assets/front/images/suggestion_2.png" alt=""/></div>
<div class="text_right_dv">
<h3>'.Input::get('nickname').'</h3>
<p><a href="javascript:void(0);">'.Input::get('ID').'</a> '.trans('messages.reset_password_popup.confirmation_message').'</p>
</div>';
echo $html;
}
}
/**
* Reset the given user's password.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postReset(Request $request)
{
$this->validate($request, [
'token' => 'required',
'ID' => 'required|email',
'password' => 'required|min:4|confirmed',
'password_confirmation' => 'required|same:password|min:4'
]);
$credentials = $request->only(
'ID', 'password', 'password_confirmation', 'token'
);
$response = Password::reset($credentials, function ($user, $password) {
$this->resetPassword($user, $password);
});
switch ($response) {
case Password::PASSWORD_RESET:
return redirect($this->redirectPath())->with('status', trans($response));
default:
return redirect()->back()
->withInput($request->only('ID'))
->withErrors(['ID' => trans($response)]);
}
}
}
答案 0 :(得分:1)
请尝试以下功能:
默认情况下,Password :: reset方法将验证密码是否匹配且是&gt; =六个字符。您可以使用Password :: validator方法自定义这些规则,该方法接受Closure。在此Closure中,您可以进行任何您希望的密码验证。请注意,您无需验证密码是否匹配,因为这将由框架自动完成。
Password::validator(function($credentials)
{
return strlen($credentials['password']) >= 4;
});
答案 1 :(得分:0)
postReset
中的getResetValidationRules
函数取代Illuminate\Foundation\Auth\ResetsPasswords.php
中的验证规则:
protected function getResetValidationRules()
{
return [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed|min:6',
];
}
要解决此问题,您需要:
getResetValidationRules
功能添加到PasswordController.php
文件中。postReset
函数(这只是复制已存在的代码)ID
表单字段重命名为email
,让Laravel处理其余字段所以,你PasswordController.php
应该只包含:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class PasswordController extends Controller
{
use ResetsPasswords;
/**
* Create a new password controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
public function getResetValidationRules()
{
return [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed|min:4',
];
}
}
答案 2 :(得分:0)
我在Laravel 5.2中注意到了同样的问题。重置密码表格需要六个或更多字符的密码,并在两个地方设置。
<强> 1。 PasswordBroker类
vendor/laravel/framework/src/Illuminate/Auth/Passwords/PasswordBroker.php:validatePasswordWithDefaults()
protected function validatePasswordWithDefaults(array $credentials)
{
list($password, $confirm) = [
$credentials['password'],
$credentials['password_confirmation'],
];
return $password === $confirm && mb_strlen($password) >= 6;
}
和
<强> 2。 ResetsPasswords Trait
vendor/laravel/framework/src/Illuminate/Foundation/Auth/ResetsPasswords.php:getResetValidationRules()
protected function getResetValidationRules()
{
return [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed|min:6',
];
}
通过实施您自己的getResetValidationRules()
和app/Http/Controllers/Auth/PasswordController.php
方法,可以轻松覆盖reset()
中的getResetValidationRules()
特征方法。
但是,为了调用您自己的PasswordBroker.php:validatePasswordWithDefaults()
方法,它看起来需要扩展PasswordBroker和PasswordBrokerManager,因为它由vendor/laravel/framework/src/Illuminate/Auth/Passwords/PasswordResetServiceProvider.php
初始化,您还需要扩展它。完成所有操作后,您可以在Laravel的服务容器中初始化新的“CustomPasswordResetServiceProvider
”,就像ResetPasswordServiceProvider
一样。
答案 3 :(得分:0)
您可以使用此gist来解决问题。您必须创建一个使用自定义密码代理管理器的新服务提供程序。设置完所有内容后,您只需将新的服务提供商添加到config \ app.php中:
'providers' => [
...
// path to your PasswordResetServiceProvider
App\Auth\PasswordResetServiceProvider::class
]