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.4] Allow adding dependent extensions to the Vallidator #18654

Merged
merged 2 commits into from
Apr 5, 2017
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
26 changes: 26 additions & 0 deletions src/Illuminate/Validation/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
*
Expand Down
29 changes: 29 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
8 changes: 5 additions & 3 deletions tests/Validation/ValidationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down