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 invalid() and valid() methods. (issue #14646) #14651

Merged
merged 8 commits into from
Aug 12, 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
20 changes: 18 additions & 2 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,22 @@ protected function validate($attribute, $rule)
}
}

/**
* Generate a key map to determine the valid
* and invalid data.
* @return array
*/
protected function getMessagesKeyMap()
{
$map = [];

foreach ($this->messages()->toArray() as $key => $message) {
$map [] = explode('.', $key)[0];
}

return array_flip(array_unique($map));
}

/**
* Returns the data which was valid.
*
Expand All @@ -498,7 +514,7 @@ public function valid()
$this->passes();
}

return array_diff_key($this->data, $this->messages()->toArray());
return array_diff_key($this->data, $this->getMessagesKeyMap());
}

/**
Expand All @@ -512,7 +528,7 @@ public function invalid()
$this->passes();
}

return array_intersect_key($this->data, $this->messages()->toArray());
return array_intersect_key($this->data, $this->getMessagesKeyMap());
}

/**
Expand Down
74 changes: 72 additions & 2 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public function testDisplayableValuesAreReplaced()
$trans->addResource('array', ['validation.in' => ':attribute must be included in :values.'], 'en', 'messages');
$customValues = [
'type' => [
'5' => 'Short',
'5' => 'Short',
'300' => 'Long',
],
];
Expand All @@ -264,7 +264,7 @@ public function testDisplayableValuesAreReplaced()
$trans->addResource('array', ['validation.in' => ':attribute must be included in :values.'], 'en', 'messages');
$customValues = [
'type' => [
'5' => 'Short',
'5' => 'Short',
'300' => 'Long',
],
];
Expand Down Expand Up @@ -2930,6 +2930,76 @@ public function testUsingSettersWithImplicitRules()
$this->assertFalse($v->passes());
}

public function testInvalidMethod()
{
$trans = $this->getRealTranslator();

$v = new Validator($trans,
[
['name' => 'John'],
['name' => null],
['name' => ''],
],
[
'*.name' => 'required',
]);

$this->assertEquals($v->invalid(), [
1 => ['name' => null],
2 => ['name' => ''],
]);

$v = new Validator($trans,
[
'name' => '',
],
[
'name' => 'required',
]);

$this->assertEquals($v->invalid(), [
'name' => '',
]);
}

public function testValidMethod()
{
$trans = $this->getRealTranslator();

$v = new Validator($trans,
[
['name' => 'John'],
['name' => null],
['name' => ''],
['name' => 'Doe'],
],
[
'*.name' => 'required',
]);

$this->assertEquals($v->valid(), [
0 => ['name' => 'John'],
3 => ['name' => 'Doe'],
]);

$v = new Validator($trans,
[
'name' => 'Carlos',
'age' => 'unknown',
'gender' => 'male',
],
[
'name' => 'required',
'gender' => 'in:male,female',
'age' => 'required|int',
]);

$this->assertEquals($v->valid(), [
'name' => 'Carlos',
'gender' => 'male',
]);
}

protected function getTranslator()
{
return m::mock('Symfony\Component\Translation\TranslatorInterface');
Expand Down