0

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?

Stan Fad
  • 1,124
  • 12
  • 23

3 Answers3

0

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

Mehran
  • 175
  • 1
  • 10
  • https://stackoverflow.com/questions/18161785/validation-of-array-form-fields-in-laravel-4-error/24985805#24985805 – Stan Fad Feb 07 '19 at 14:01
0

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+

bmatovu
  • 3,756
  • 1
  • 35
  • 37
0

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

Stan Fad
  • 1,124
  • 12
  • 23