Koen Betsens
Koen Betsens

Reputation: 88

Validator extend method in Laravel

I'm trying out a creative approach towards the Validator::extend method. There is 1 function from a custom class SchemaValidator that we need in the Validator class.

in SchemaValidator.php

public function getValidated()
{
    return array_intersect_key ($this->getData(), $this->getRules());
}

in app/start.php

Validator::extend('getValidated', 'SchemaValidator@getValidated');

And finally, where we call the regular Validator class

$validator = Validator::make ($input, $rules);

// ... some error checks

return $validator->getValidated();

However, this fails. Did I forget anything? Or am I right in my fears that I'm just abusing Validator::extend ?

edit: The error output

BadMethodCallException
Method [getValidated] does not exist.

Upvotes: 1

Views: 2024

Answers (1)

Faore
Faore

Reputation: 11

Try using the full path to your SchemaValidator class. IE:

Validator::extend('getValidated', '\App\Services\SchemaValidator@getValidated');

Upvotes: 1

Related Questions