validation rule required_if requires | web-beast.com | Message Validation rule required_if requires at least 2 parameters

validation rule required_if requires | web-beast.com

<?php
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Controller;

public function submitForm(Request $request)
{
    $request->validate([
        'email' => Rule::requiredIf(!$request->input('phone')),
        'phone' => Rule::requiredIf(!$request->input('email')),
    ], [
        'email.required_if' => 'Please provide an email or phone number.',
        'phone.required_if' => 'Please provide an email or phone number.',
    ]);
}
?>

The validation rule you provided is used to validate either the 'email' or 'phone' field, depending on which one is not provided. If the 'phone' field is not provided, the 'email' field is required, and vice versa.

This rule ensures that at least one of the two fields is required to be filled out. If the 'phone' field is present, the 'email' field becomes optional. Similarly, if the 'email' field is present, the 'phone' field becomes optional.

This validation rule can be used when you have a form where the user can provide either an email address or a phone number, but not both. It ensures that the user provides at least one of the contact details.

If the validation fails and both the 'email' and 'phone' fields are empty or both are filled, you can display an error message to the customer indicating that they need to provide either an email or a phone number, but not both.


$request->validate([
    'email' => Rule::requiredIf(!$request->input('phone')),
    'phone' => Rule::requiredIf(!$request->input('email')),
], [
    'email.required_if' => 'Please provide an email or phone number.',
    'phone.required_if' => 'Please provide an email or phone number.',
]);



Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.