Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Created November 30, 2015 21:38
Show Gist options
  • Save cviebrock/bab707a338cb03ec19de to your computer and use it in GitHub Desktop.
Save cviebrock/bab707a338cb03ec19de to your computer and use it in GitHub Desktop.
Validate a list of things
<?php
namespace App\Validators;
use Illuminate\Contracts\Validation\Validator;
class ListOfValidator
{
public function validate($attribute, $value, $parameters, Validator $validator)
{
// extract all the values
$values = explode(',', $value);
// reconstitute the parameters
$newRules = join('|', $parameters);
// revalidate each value
foreach ($values as $newValue) {
$newValidator = \Validator::make(
['value' => $newValue],
['value' => $newRules]
);
if ($newValidator->fails()) {
return false;
};
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment