我的表正在更新,但在 Laravel 6 更新之前未验证



我是第一次使用Laravel,版本6.2。我的代码适用于更新数据库上的表,但它在更新之前不会验证数据。这是我第一次需要自己验证我的表单(不是默认的 laravel 身份验证(,我认为我缺少一些基本的东西来让它工作。

我正在尝试使用当前密码 - 新密码 - 确认新密码制作典型的更改密码表单。下面是我的路线,我的控制器和我的观点。

路线

Route::get('/cambiarclave', 'AuthChangePasswordController@showChangeForm');
Route::post('/cambiarclave', 'AuthChangePasswordController@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>

你在这里错过了执行validator方法。您应该添加如下代码:

$validator = $this->validator($data);
if ($validator->fails()) {
return back()
->withErrors($validator)
->withInput();
}

$user = User::find(auth()->user()->id);

行以运行验证。

相关内容

  • 没有找到相关文章

最新更新