0
votes

I've created custom validator:

namespace App\Validators;

class PhoneValidationRule extends \Illuminate\Validation\Validator {

    public function validatePhone($attribute, $value, $parameters)
    {
        return preg_match("/^[\+]?[-()\s\d]{4,17}$/", $value);
    }
}

and registered it:

class ValidatorServiceProvider extends ServiceProvider {


    public function boot()
    {
        \Validator::resolver(function($translator, $data, $rules, $messages)
        {
            return new PhoneValidationRule($translator, $data, $rules, $messages);
        });
    }
...

and it works fine if i call it for field:

        $validator = Validator::make($input, [
            'emails' => 'required|each:email',
            'phone' => 'required|phone',
        ]);

but when i try to apply it for array:

        $validator = Validator::make($input, [
            'emails' => 'required|each:email',
            'phones' => 'required|each:phone',
        ]);

i get error message:

error: {type: "BadMethodCallException", message: "Method [validateEach] does not exist.",…} file: "/home/.../vendor/laravel/framework/src/Illuminate/Validation/Validator.php" line: 2564 message: "Method [validateEach] does not exist." type: "BadMethodCallException"

what i'm doing wrong?

3
This is the first time I see each. Is this a custom rule?Mozammil
@Mozammil no, it's native - check the line 231Stan Fad

3 Answers

0
votes

Your problem is this part: required|each.

There is no such thing as a each validation rule. Take a look at the docs for a list of available validation rules: docs

0
votes

Validating an individual field

$validator = Validator::make($request->all(), [
    'email' => 'required|email',
    'phone' => 'required|phone',
]);

Validating arrays

$validator = Validator::make($request->all(), [
    'emails' => 'required|array',
    'emails.*' => 'email',
    'phones' => 'required|array',
    'phones.*' => 'phone',
]);

* Laravel 5.3+

0
votes

each()

The problem was partially solved by straight calling native method $v->each() for custom rule phone:

$validator = Validator::make($input, [
   'phones' => 'required|array',
]);

$validator->each('phones', ['required', 'phone']);

but it allows you to iterate validation only for arrays of values but not objects