Search code examples
phplaravellaravel-6laravel-validationlaravel-6.2

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


I'm using Laravel for the first time, version 6.2. My code is working well for updating the table on the DB but it does not validate the data before update. This is the first time I need to validate my form on my own (not the default laravel auth) and I think I'm missing something basic to get it to work.

I'm trying to make the typical change password form with current-password - new password - confirm the new password. Below is my route, my controller and my view.

Routes

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>

Solution

  • You miss here executing your validator method. You should add code like this:

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

    after

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

    line in your controller to run validation.