我第一次使用 Laravel,版本 6.2。我的代码可以很好地更新数据库上的表,但它在更新之前不验证数据。这是我第一次需要自己验证我的表单(不是默认的 laravel auth),我认为我缺少一些基本的东西来让它工作。
我正在尝试使用当前密码 - 新密码 - 确认新密码来制作典型的更改密码表格。下面是我的路线,我的控制器和我的观点。
路线
Route::get('/cambiarclave', 'Auth\ChangePasswordController@showChangeForm');
Route::post('/cambiarclave', 'Auth\ChangePasswordController@changePassword')->name('cambiarclave');
更改密码控制器.php
class ChangePasswordController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function showChangeForm()
{
return view('auth.cambiarclave');
}
protected function validator(array $data)
{
return Validator::make($data, [
'clave-actual' => ['required', 'string', 'min:6'],
'nueva-clave' => ['required', 'string', 'min:6', 'confirmed'],
]);
}
public function changePassword(request $request)
{
$data = $request->all();
$user = User::find(auth()->user()->id);
if (!Hash::check($data['clave-actual'], $user->password)) {
return back()->with('error', 'You have entered wrong password');
}
$user_id = $user;
$obj_user = User::find($user_id);
$obj_user->password = \Hash::make($request->input('nueva-clave'));
$obj_user->save();
auth()->logout();
return redirect('/ingreso');
}
}
cambiarclave.blade.php
<form method="POST" id="change-password" role="form" name="change-password" action="{{ route('cambiarclave') }}"
novalidate>
@csrf
<div class="form-group row">
<label for="clave-actual" class="col-md-6 col-form-label text-md-right">{{ __('Clave actual') }}</label>
<div class="col-md-5">
<input type="password" class="form-control" id="clave-actual" name="clave-actual" placeholder="Password"
required autofocus>
@if ($errors->has('clave-actual'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('clave-actual') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="nueva-clave" class="col-md-6 col-form-label text-md-right">{{ __('Nueva clave') }}</label>
<div class="col-md-5">
<input type="password" class="form-control" id="nueva-clave" name="nueva-clave" placeholder="Password"
required autofocus>
@if ($errors->has('nueva-clave'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('nueva-clave') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="nueva-clave-confirm"
class="col-md-6 col-form-label text-md-right">{{ __('Confirmar nueva clave') }}</label>
<div class="col-md-5">
<input type="password" class="form-control" id="nueva-clave-confirm" name="nueva-clave-confirm"
placeholder="Password" required autofocus>
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-4">
<button type="submit" class="btn btn-primary w-50">
<p class="h5 p-0 mt-1">{{ __('Cambiar') }}</p>
</button>
</div>
</div>
</form>