etranz
etranz

Reputation: 1253

How do I validate email in array laravel

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

Upvotes: 1

Views: 314

Answers (1)

Madan Sapkota
Madan Sapkota

Reputation: 26081

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 be applied within the controller validate method.

Upvotes: 1

Related Questions