-
-
Save aravael/1031a3d911cfd8e69e15039df6ad9c7c to your computer and use it in GitHub Desktop.
Laravel 5.4 Comma Separated Email Validation (Transforming Data)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Requests; | |
use Illuminate\Foundation\Http\FormRequest; | |
class EmailRequest extends FormRequest | |
{ | |
/** | |
* Determine if the user is authorized to make this request. | |
* | |
* @return bool | |
*/ | |
public function authorize() | |
{ | |
return true; | |
} | |
/** | |
* @inheritDoc | |
*/ | |
protected function prepareForValidation() | |
{ | |
$this->merge(['email' => explode(',', $this->email)]); | |
} | |
/** | |
* Get the validation rules that apply to the request. | |
* | |
* @return array | |
*/ | |
public function rules() | |
{ | |
return [ | |
'email' => 'required|bail|array', | |
'email.*' => 'email' | |
]; | |
} | |
public function messages() | |
{ | |
return [ | |
'email.*' => ':attribute is not a valid email address' | |
]; | |
} | |
public function attributes() | |
{ | |
$attributes = array(); | |
foreach ($this->email as $index=>$email) { | |
$attributes['email.'.$index] = $email; | |
} | |
return $attributes; | |
} | |
/** | |
* This is part of an API which should only | |
* return JSON | |
*/ | |
public function wantsJson() | |
{ | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment