Created
December 20, 2017 15:23
-
-
Save zerodahero/c7bc9e69c10d904b05d9a7f60179a608 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->replace(['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
Yep! Not sure how much of this is still valid considering so much has changed since 5.4. Yes, merge would work better here if you've got more than just email coming it.