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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3114,7 +3114,11 @@ protected function callExtension($rule, $parameters)
*/
protected function callClassBasedExtension($callback, $parameters)
{
list($class, $method) = explode('@', $callback);
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?

}

return call_user_func_array([$this->container->make($class), $method], $parameters);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,20 @@ public function testClassBasedCustomValidators()
$this->assertEquals('foo!', $v->messages()->first('name'));
}

public function testClassBasedCustomValidatorsUsingInvoke()
{
$trans = $this->getRealTranslator();
$trans->addResource('array', ['validation.foo' => 'foo!'], 'en', 'messages');
$v = new Validator($trans, ['name' => 'taylor'], ['name' => 'foo']);
$v->setContainer($container = m::mock('Illuminate\Container\Container'));
$v->addExtension('foo', 'Foo');
$container->shouldReceive('make')->once()->with('Foo')->andReturn($foo = m::mock('StdClass'));
$foo->shouldReceive('__invoke')->once()->andReturn(false);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('foo!', $v->messages()->first('name'));
}

public function testCustomImplicitValidators()
{
$trans = $this->getRealTranslator();
Expand Down