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] Fix a case with parameters of explicit depending rules #13058

Merged
merged 1 commit into from
Apr 7, 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
28 changes: 16 additions & 12 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,10 @@ protected function validate($attribute, $rule)
return;
}

// First we will get the numeric keys for the given attribute in case the field is nested in
// First we will get the correct keys for the given attribute in case the field is nested in
// an array. Then we determine if the given rule accepts other field names as parameters.
// If so, we will replace any asterisks found in the parameters with the numeric keys.
if (($keys = $this->getNumericKeys($attribute)) &&
// If so, we will replace any asterisks found in the parameters with the correct keys.
if (($keys = $this->getExplicitKeys($attribute)) &&
$this->dependsOnOtherFields($rule)) {
$parameters = $this->replaceAsterisksInParameters($parameters, $keys);
}
Expand Down Expand Up @@ -2576,17 +2576,21 @@ protected function dependsOnOtherFields($rule)
}

/**
* Get the numeric keys from an attribute flattened with dot notation.
* Get the explicit keys from an attribute flattened with dot notation.
*
* E.g. 'foo.1.bar.2.baz' -> [1, 2]
* E.g. 'foo.1.bar.spark.baz' -> [1, 'spark'] for 'foo.*.bar.*.baz'
*
* @param string $attribute
* @return array
*/
protected function getNumericKeys($attribute)
protected function getExplicitKeys($attribute)
{
if (preg_match_all('/\.(\d+)\./', $attribute, $keys)) {
return $keys[1];
$pattern = str_replace('\*', '([^\.]+)', preg_quote($this->getPrimaryAttribute($attribute)));

if (preg_match('/^'.$pattern.'/', $attribute, $keys)) {
array_shift($keys);

return $keys;
}

return [];
Expand Down Expand Up @@ -2629,7 +2633,7 @@ protected function extractDataFromPath($attribute)
}

/**
* Replace each field parameter which has asterisks with the given numeric keys.
* Replace each field parameter which has asterisks with the given keys.
*
* @param array $parameters
* @param array $keys
Expand All @@ -2643,17 +2647,17 @@ protected function replaceAsterisksInParameters(array $parameters, array $keys)
}

/**
* Replace asterisks with numeric keys.
* Replace asterisks with explicit keys.
*
* E.g. 'foo.*.bar.*.baz', [1, 2] -> foo.1.bar.2.baz
* E.g. 'foo.*.bar.*.baz', [1, 'spark'] -> foo.1.bar.spark.baz
*
* @param string $field
* @param array $keys
* @return string
*/
protected function replaceAsterisksWithKeys($field, array $keys)
{
return vsprintf(str_replace('*', '%d', $field), $keys);
return vsprintf(str_replace('*', '%s', $field), $keys);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2516,6 +2516,12 @@ public function testValidateImplicitEachWithAsterisksRequiredWith()
$this->assertTrue($v->messages()->has('foo.0.name'));
$this->assertTrue($v->messages()->has('foo.1.name'));

$v = new Validator($trans, ['fields' => [
'fr' => ['name' => '', 'content' => 'ragnar'],
'es' => ['name' => '', 'content' => 'lagertha'],
]], ['fields.*.name' => 'required_with:fields.*.content']);
$this->assertFalse($v->passes());

// nested required_with fails
$v = new Validator($trans, ['foo' => [
['bar' => [
Expand Down