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.3] Fix an issue with sometimes when it comes to wildcard rules #16826

Merged
merged 2 commits into from
Dec 16, 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
8 changes: 7 additions & 1 deletion src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,13 @@ protected function presentOrRuleIsImplicit($rule, $attribute, $value)
protected function passesOptionalCheck($attribute)
{
if ($this->hasRule($attribute, ['Sometimes'])) {
return array_key_exists($attribute, Arr::dot($this->data))
$data = Arr::dot($this->initializeAttributeOnData($attribute));

$data = array_merge($data, $this->extractValuesForWildcards(
$data, $attribute
));

Copy link
Contributor

Choose a reason for hiding this comment

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

@themsaid $data is needed only when the attribute has sometimes rule. Could you move data extraction logic inside if condition?

return array_key_exists($attribute, $data)
|| in_array($attribute, array_keys($this->data));
}

Expand Down
7 changes: 6 additions & 1 deletion tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2475,13 +2475,18 @@ public function testValidateImplicitEachWithAsterisks()
$this->assertFalse($v->passes());
}

public function testSometimesFailOnEmptyArrayInImplicitRules()
public function testSometimesOnArraysInImplicitRules()
{
$trans = $this->getIlluminateArrayTranslator();

$data = ['names' => [['second' => []]]];
$v = new Validator($trans, $data, ['names.*.second' => 'sometimes|required']);
$this->assertFalse($v->passes());

$data = ['names' => [['second' => ['Taylor']]]];
$v = new Validator($trans, $data, ['names.*.second' => 'sometimes|required|string']);
$this->assertFalse($v->passes());
$this->assertEquals(['validation.string'], $v->errors()->get('names.0.second'));
}

public function testValidateImplicitEachWithAsterisksForRequiredNonExistingKey()
Expand Down