fideloper / Implementing-Laravel

Companion application to the e-book Implementing Laravel
173 stars 40 forks source link

Bug checking "unique" with Validator #18

Closed grena closed 10 years ago

grena commented 10 years ago

Hi !

By adopting the Repo pattern, I'm facing an issue with validating a "unique" field with validator. Here is my rule :

protected $rules = array(
        'name'    => 'required',
        'prefix'  => 'max:4|unique:ateliers'
);

And the error :

ERROR: exception 'RuntimeException' with message 'Presence verifier has not been set.' in (...)/vendor/laravel/framework/src/Illuminate/Validation/Validator.php:1726

What I understood is that the PresenceVerifier is used to count elements in database (to ensure it's unique), but since we use RepoInterfaces, it's broken.

How can I deal with that ?

fideloper commented 10 years ago

Hi! I've used plenty of database-based validation using this pattern. I'm not sure I have quite enough information to let you know why this might be not working for you.

However, I have a theory. If you check issue #14, you'll see that for any validator class, I use a Service Provider and provide the validator service class with Laravel's validator via the Application container: $app['validator']. This gives the validator class a complete (all dependencies, such as PresenceVerifier, included) Validator object to use.

You can see the code on how $app['validator'] is created, and also how the Presence Verifier is added as well.

Let me know if I'm off-base in my guess here!

(It's also definitely possible that updates to Laravel 4.1 may have created some breaking changes to my code, in which case I'll fix it up in the code in book.)

longilineo commented 10 years ago

In the abstract validator constructor I've: $this->validator = \App::make['validator']; This resolve Presence Verifier and Translator Interface issues for me.