Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate multiple email in laravel validation?

I have done all the things for the validation for the variable in laravel but for emails I got one simple problem.

From doc of Laravel,

'email' => 'required|email'

I got to know this is for only one email address but for like,

[email protected],[email protected], def@ghi,com

When I send array of the email i still get email is not a valid email. I have done more like,

'email' => 'required|email|array'

But I still got error. can any body help.

Thanks,

like image 679
Subhod30 Avatar asked Apr 05 '15 06:04

Subhod30


1 Answers

We can achieve this without custom validation,We can overridden a method prepareForValidation

protected function prepareForValidation() 
{
   //Here email we are reciving as comma seperated so we make it array
   $this->merge(['email' => explode(',', rtrim($this->email, ','))]);
}

Then above function will call automatically and convert email-ids to array, after that use array validation rule

public function rules()
 {
     return [
        'email.*' => 'required|email'
     ];
 }
like image 108
Elby Avatar answered Oct 14 '22 09:10

Elby