我的 table 正在更新,但在 Laravel 6 更新之前未经过验证

My table is updating but not validated before update in Laravel 6

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

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

路线

Route::get('/cambiarclave', 'Auth\ChangePasswordController@showChangeForm');
Route::post('/cambiarclave', 'Auth\ChangePasswordController@changePassword')->name('cambiarclave');

ChangePasswordController.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);

在您的控制器中输入 运行 验证。