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?

2
  • 1
    This is the first time I see each. Is this a custom rule? Commented Feb 7, 2019 at 12:01
  • @Mozammil no, it's native - check the line 231 Commented Feb 8, 2019 at 12:27

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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+

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.