You may occasionally wish to not validate a given field if another field has a given value. You may accomplish this using the exclude_if
validation rule. In this example, the appointment_date
and doctor_name
fields will not be validated if the has_appointment
field has a value of false
:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($data, [
'has_appointment' => 'required|boolean',
'appointment_date' => 'exclude_if:has_appointment,false|required|date',
'doctor_name' => 'exclude_if:has_appointment,false|required|string',
]);
If we want to apply unique validation when $request->type="register" wen $request->type="forgot_password" unique validation will be not work. In below eample
$validator = Validator::make($request->all(), [
'type' => 'required|in:register,forgot_password',
'email' => 'exclude_if:type,"forgot_password"|required|unique:users,email'
]);
For more details
Comments
Post a Comment
If you any doubt of any topic then you can comment me.