Search code examples
laravelvalidationlaravel-6laravel-validation

Validator function is not resulting error on edit page


The following has a UserController@update_personal method which uses a custom validation.

UserController

public function update_personal(Request $request, User $user)
{
    $validator = validator($request->all());
    if ($validator->fails()) {
        return back()
            ->withErrors($validator)
            ->withInput();
    }

    $userUpdate = User::where('id', $user->id)
        ->update([
            'firstname' => $request->input('firstname'),
            'lastname' => $request->input('lastname'),
            'birth_date' => $request->input('birth_date'),
            'phone' => $request->input('phone')
        ]);

    if ($userUpdate) {
        return redirect()
            ->route('users.index', ['user' => $user->id])
            ->with('success', 'Personal details updated successfully');
    }

    return back()->withInput();
}

Validator method

protected function validator(array $data)
{
    return Validator::make($data, [
        'firstname' => ['required', 'string', 'max:50', 'alpha'],
        'lastname' => ['required', 'string', 'max:50', 'alpha'],
        'birth_date' => ['required', 'date'],
        'phone' => ['required', new PhoneNumber()],
    ]);
}

Routes

// Routes for UserController
Route::get('/users/{user}/personal', 'UserController@edit_personal')->name('users.edit-personal');
Route::put('/users/{user}/personal', 'UserController@update_personal')->name('users.update-personal');

Route::get('/users/{user}/email', 'UserController@edit_email')->name('users.edit-email');
Route::put('/users/{user}/email', 'UserController@update_email')->name('users.update-email');

Route::get('/users/{user}/password', 'UserController@edit_password')->name('users.edit-password');
Route::put('/users/{user}/password', 'UserController@update_password')->name('users.update-password');
Route::resource('users', 'UserController');

I try to display errors below every input field rather than display all of them altogether. This is one of my blade entries for firstname:

<div class="form-group col-md-6">
    <label for="firstname">First Name</label>
    <input id="firstname" type="text"
           class="form-control @error('firstname') is-invalid @enderror"
           name="firstname" value="{{ $user->firstname }}" required
           autocomplete="given-name" autofocus>
    @error('firstname')
    <span class="invalid-feedback" role="alert">
        <strong>{{ $message }}</strong>
    </span>
    @enderror
</div>

The problem is whatever the value I type for input field 'firstname', the database just updates and shows me the success page, not showing an error.

For example, I have changed firstname from 'Kate' to 'Kate1234' and it works. But I've added an 'Alpha' tag to validator(). So it should not let me update and show the error below input. Any idea what is the problem here?


Solution

  • The problem here is that you don't use validator method anywhere from controller but you use validator helper that requires passing rules as 2nd argument. The simplest solution in this case is changing:

    $validator = validator($request->all());
    

    into

    $validator = $this->validator($request->all());
    

    to use your custom controller method that will create validator and pass rules as you implemented