Home > Software engineering >  Laravel required_with throwing error even when its supposed to be ignored
Laravel required_with throwing error even when its supposed to be ignored

Time:09-17

Basic user profile update page with an optional password change. Only if theyre trying to change their password, does it request the current password.

Controller:

    $this->validate($request, [
        'name'      => 'required',
        'email'     => 'required|email',
        'new_password'  => 'confirmed',
        'current_password'  => 'required_with:new_password|current_password'
    ]);

    $user->name     = $request->get('name');
    $user->surname  = $request->get('surname');
    $user->email    = $request->get('email');
    if($request->get('password') !== ''){
        $user->password = Hash::make($request->get('new_password'));
    }
    $user->save();

But validation keeps tripping up and giving me the error "validation.current_password"

View (scaled back for readability):

        @if($errors)
            @foreach ($errors->all() as $error)
                <div>{{ $error }}</div>
            @endforeach
        @endif

{!! Form::model($user, ['method'=>'PATCH', 'action'=>['UserController@update', $user->id], 'class'=>'kt-form kt-form--state', 'id'=>'form-user-profile', 'files' => true]) !!}
{!! Form::text('name', null, ['class'=>'form-control']) !!}
{!! Form::password('current_password', ['class'=>'form-control','placeholder'=>__('Current Password')]) !!}
{!! Form::password('new_password', ['class'=>'form-control','placeholder'=>__('New Password')]) !!}
{!! Form::password('new_password_confirmation', ['class'=>'form-control','placeholder'=>__('Verify New Password')]) !!}

{{ Form::button(__('Submit'), ['type' => 'submit', 'class' => 'btn btn-success']) }}
{{ Form::button(__('Reset'), ['type' => 'reset', 'class' => 'btn btn-secondary']) }}

{!! Form::close() !!}

As per https://laravel.com/docs/9.x/validation#rule-required-with required_with only needs to be present if new_password is present. So why am I getting a "validation.current_password" error when I only update my "name" field (for example).

CodePudding user response:

From the error you're getting, it is somehow clear that the required_with is not the rule behind the error, it actually is the current_password rule.

According to the docs, the current_password rule does the following check

The field under validation must match the authenticated user's password

So with that being said, it seems you're not typing the correct password of the currently logged in user which trigger the validation error of the current_password rule.

A quick fix could be:

  • starting by adding the nullable validation rule on both new_password and current_password fields so the validation can pass when the fields are empty.
  • use required_unless rule instead of required_with which allows you to verify whether new_password field is empty (null) or not.

required_unless: The field under validation must be present and not empty unless the anotherfield field is equal to any value. This also means anotherfield must be present in the request data unless value is null. If value is null (required_unless:name,null), the field under validation will be required unless the comparison field is null or the comparison field is missing from the request data.

Here's a code sample illustrating what's being said:

$this->validate($request, [
  'name' => 'required',
  'email'     => ['required', 'email'],
  'new_password'  => ['nullable', 'confirmed'],
  'current_password'  => ['nullable', 'required_unless:new_password,null', 'current_password']
]);

In the above code sample i used arrays instead of strings to specify the rules. This doesn't have anything to do with the solution, it is used to make the changes clearer and the code more readable

Bonus Tip: you're getting validation.current_password as the error message is due to the lang files that contain the validation message being deleted. TheEN version of those files should be under resources/lang/en/validation.php. You may simply download that file from GitHub and place it there or you may use your own custom error messages.

Learn more about Validation Rules on Laravel docs.

  • Related