Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Allow custom validators to be called with out function name #13444

Merged
merged 1 commit into from
May 9, 2016
Merged

[5.2] Allow custom validators to be called with out function name #13444

merged 1 commit into from
May 9, 2016

Conversation

joshhornby
Copy link
Contributor

This would class based custom validators to simply call the class instead of having to define a function inside the class.

Validator::extend('postcode', PostcodeValidator::class);

@taylorotwell
Copy link
Member

Typically we wouldn't call invoke but rather some convention like validate.

if (Str::contains($callback, '@')) {
list($class, $method) = explode('@', $callback);
} else {
list($class, $method) = [$callback, '__invoke'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surely it would be better to let php call that dynamically itself?

@joshhornby
Copy link
Contributor Author

@taylorotwell

PostcodeValidator::class vs App\Validator\PostcodeValidator@validate

For me the ::class way is cleaner and easier to use with modern IDEs. Is there any reason not to allow both ways?

@taylorotwell
Copy link
Member

I'm saying if you leave off the method we shouldn't call invoke, we should call validate.

@joshhornby
Copy link
Contributor Author

Isn't this hiding magic inside the framework? Because if you call PostcodeValidator::class you'd expect it to call invoke?

@joshhornby
Copy link
Contributor Author

@taylorotwell Ok, if we should call validate how do you feel about having an interface for custom validators to implement?

@taylorotwell
Copy link
Member

Interface not needed. Just define the method.

@taylorotwell
Copy link
Member

An interface would just be documentation at that point and waste loading time. If you don't define the method, you're going to get an error anyways. Embrace the dynamic nature of PHP :)

@taylorotwell taylorotwell merged commit e678f36 into laravel:5.2 May 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants