How can I prevent Laravel from validating certain columns when editing an existing user? I'm trying to update a guest's information, but I want to ignore the phone and email fields if no changes have been made to the guest's details. However, when creating a new guest, it should properly check for uniqueness. How can I achieve this? I've been struggling with this issue for a while.
Here are my form validation rules:
return [
'first_name' => 'required|string|max:255',
'last_name' => 'nullable|string|max:255',
'other_names' => 'nullable|string|max:255',
'city' => 'nullable|string|max:255',
'country' => 'nullable|string|max:255',
'email' => ['required_if:phone,null', Rule::unique('guests')->whereNot('email',null)->where('hotel_id',auth()->user()->hotel_id) ],
'phone' => ['required_if:email,null', Rule::unique('guests')->where('phone','!=', $request->phone)->where('hotel_id',auth()->user()->hotel_id) ],
'address' => 'nullable|string|max:255',
'other_phone' => 'nullable|numeric:11',
];
Any assistance would be greatly appreciated.
Answers
To achieve the behavior you described, you can conditionally apply validation rules based on whether you are creating a new guest or updating an existing one. Laravel provides a convenient way to define conditional rules using the sometimes
method.
Here's how you can modify your validation rules to achieve your requirements:
use Illuminate\Validation\Rule;
// Define the base validation rules for creating a new guest
$rules = [
'first_name' => 'required|string|max:255',
'last_name' => 'nullable|string|max:255',
'other_names' => 'nullable|string|max:255',
'city' => 'nullable|string|max:255',
'country' => 'nullable|string|max:255',
'address' => 'nullable|string|max:255',
'other_phone' => 'nullable|numeric:11',
];
// Apply additional rules conditionally for creating a new guest
if ($request->isMethod('post')) {
$rules['email'] = [
'required_if:phone,null',
Rule::unique('guests')->whereNot('email', null)->where('hotel_id', auth()->user()->hotel_id),
];
$rules['phone'] = [
'required_if:email,null',
Rule::unique('guests')->where('phone', '!=', $request->phone)->where('hotel_id', auth()->user()->hotel_id),
];
}
// Apply additional rules conditionally for updating an existing guest
if ($request->isMethod('put')) {
$rules['email'] = [
'nullable',
'required_without:phone', // Make email field optional if phone field is provided
Rule::unique('guests')->whereNot('email', null)->where('hotel_id', auth()->user()->hotel_id),
];
$rules['phone'] = [
'nullable',
'required_without:email', // Make phone field optional if email field is provided
Rule::unique('guests')->where('phone', '!=', $request->phone)->where('hotel_id', auth()->user()->hotel_id),
];
}
// Validate the request using the defined rules
$request->validate($rules);
In this code:
- We define the base validation rules for common fields.
- For creating a new guest (
POST
request), we conditionally apply the validation rules foremail
andphone
. - For updating an existing guest (
PUT
request), we conditionally apply the validation rules foremail
andphone
while making these fields optional if the other field is provided.
This approach allows you to handle validation rules differently based on the HTTP method used for the request, allowing you to ignore certain columns when updating an existing user.