Skip to content

Instantly share code, notes, and snippets.

@aravael
Forked from zerodahero/EmailRequest.php
Last active February 24, 2021 12:20
Show Gist options
  • Save aravael/1031a3d911cfd8e69e15039df6ad9c7c to your computer and use it in GitHub Desktop.
Save aravael/1031a3d911cfd8e69e15039df6ad9c7c to your computer and use it in GitHub Desktop.
Laravel 5.4 Comma Separated Email Validation (Transforming Data)
<?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