diff --git a/src/Illuminate/Validation/Factory.php b/src/Illuminate/Validation/Factory.php index c415bb181dcd..d11fa9912664 100755 --- a/src/Illuminate/Validation/Factory.php +++ b/src/Illuminate/Validation/Factory.php @@ -45,6 +45,13 @@ class Factory implements FactoryContract */ protected $implicitExtensions = []; + /** + * All of the custom dependent validator extensions. + * + * @var array + */ + protected $dependentExtensions = []; + /** * All of the custom validator message replacers. * @@ -162,6 +169,8 @@ protected function addExtensions(Validator $validator) // array of data that is given to a validator instances via instantiation. $validator->addImplicitExtensions($this->implicitExtensions); + $validator->addDependentExtensions($this->dependentExtensions); + $validator->addReplacers($this->replacers); $validator->setFallbackMessages($this->fallbackMessages); @@ -201,6 +210,23 @@ public function extendImplicit($rule, $extension, $message = null) } } + /** + * Register a custom implicit validator extension. + * + * @param string $rule + * @param \Closure|string $extension + * @param string $message + * @return void + */ + public function extendDependent($rule, $extension, $message = null) + { + $this->dependentExtensions[$rule] = $extension; + + if ($message) { + $this->fallbackMessages[Str::snake($rule)] = $message; + } + } + /** * Register a custom implicit validator message replacer. * diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index c44c5d5211b2..9a95c5d2ece7 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -812,6 +812,21 @@ public function addImplicitExtensions(array $extensions) } } + /** + * Register an array of custom implicit validator extensions. + * + * @param array $extensions + * @return void + */ + public function addDependentExtensions(array $extensions) + { + $this->addExtensions($extensions); + + foreach ($extensions as $rule => $extension) { + $this->dependentRules[] = Str::studly($rule); + } + } + /** * Register a custom validator extension. * @@ -838,6 +853,20 @@ public function addImplicitExtension($rule, $extension) $this->implicitRules[] = Str::studly($rule); } + /** + * Register a custom dependent validator extension. + * + * @param string $rule + * @param \Closure|string $extension + * @return void + */ + public function addDependentExtension($rule, $extension) + { + $this->addExtension($rule, $extension); + + $this->dependentRules[] = Str::studly($rule); + } + /** * Register an array of custom validator message replacers. * diff --git a/tests/Validation/ValidationFactoryTest.php b/tests/Validation/ValidationFactoryTest.php index ba698eca6b85..a9bb826641c2 100755 --- a/tests/Validation/ValidationFactoryTest.php +++ b/tests/Validation/ValidationFactoryTest.php @@ -34,20 +34,22 @@ public function testMakeMethodCreatesValidValidator() }; $factory->extend('foo', $noop1); $factory->extendImplicit('implicit', $noop2); + $factory->extendDependent('dependent', $noop3); $factory->replacer('replacer', $noop3); $factory->setPresenceVerifier($presence); $validator = $factory->make([], []); - $this->assertEquals(['foo' => $noop1, 'implicit' => $noop2], $validator->extensions); + $this->assertEquals(['foo' => $noop1, 'implicit' => $noop2, 'dependent' => $noop3], $validator->extensions); $this->assertEquals(['replacer' => $noop3], $validator->replacers); $this->assertEquals($presence, $validator->getPresenceVerifier()); $presence = m::mock(PresenceVerifierInterface::class); $factory->extend('foo', $noop1, 'foo!'); $factory->extendImplicit('implicit', $noop2, 'implicit!'); + $factory->extendImplicit('dependent', $noop3, 'dependent!'); $factory->setPresenceVerifier($presence); $validator = $factory->make([], []); - $this->assertEquals(['foo' => $noop1, 'implicit' => $noop2], $validator->extensions); - $this->assertEquals(['foo' => 'foo!', 'implicit' => 'implicit!'], $validator->fallbackMessages); + $this->assertEquals(['foo' => $noop1, 'implicit' => $noop2, 'dependent' => $noop3], $validator->extensions); + $this->assertEquals(['foo' => 'foo!', 'implicit' => 'implicit!', 'dependent' => 'dependent!'], $validator->fallbackMessages); $this->assertEquals($presence, $validator->getPresenceVerifier()); } diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index c2ba2093aab3..0aadad34ca6d 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -2518,6 +2518,21 @@ public function testCustomImplicitValidators() $this->assertTrue($v->passes()); } + public function testCustomDependentValidators() + { + $trans = $this->getIlluminateArrayTranslator(); + $v = new Validator($trans, + [ + ['name' => 'Jamie', 'age' => 27], + ], + ['*.name' => 'dependent_rule:*.age'] + ); + $v->addDependentExtension('dependent_rule', function ($name) use ($v) { + return array_get($v->getData(), $name) == 'Jamie'; + }); + $this->assertTrue($v->passes()); + } + /** * @expectedException InvalidArgumentException */