Home > Enterprise >  How do I validate email in array laravel
How do I validate email in array laravel

Time:10-24

I am sending data request in this format to my laravel backend application

array:2 [
  0 => array:2 [
    "email" => "[email protected]"
    "role_id" => 2
  ]
  1 => array:2 [
    "email" => "[email protected]"
    "role_id" => 3
  ]
]

How do I validate the email and role_id in laravel

CodePudding user response:

If your validation is generated using php artisan make:request use the rules method.

/**
 * Get the validation rules that apply to the request.
 * @return array
 */
public function rules(): array
{
    return [
        'array_name' => ['required', 'array'],
        'array_name.*.email' => ['required', 'email'],
        'array_name.*.role_id' => ['required', '...'],
    ];
}

The same validation rules can apply the same rules within the controller validate method.

CodePudding user response:

Try this

$input = $request->validate([
    'email.*' => 'required|email',
    'role_id.*' => 'required',
]);

Just add .* after the name.

  • Related