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