Skip to content

Commit

Permalink
[5.4] Attempt to simplify validation with an edge case (#18376)
Browse files Browse the repository at this point in the history
* Attempt to simplify validation with an edge case

*    update style

*     remove unused parameter

*    fix style
  • Loading branch information
themsaid authored and taylorotwell committed Mar 16, 2017
1 parent 8ebb5b8 commit d8ba110
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ protected function isValidatable($rule, $attribute, $value)
{
return $this->presentOrRuleIsImplicit($rule, $attribute, $value) &&
$this->passesOptionalCheck($attribute) &&
$this->isNotNullIfMarkedAsNullable($attribute, $value) &&
$this->isNotNullIfMarkedAsNullable($rule, $attribute) &&
$this->hasNotFailedPreviousRuleIfPresenceRule($rule, $attribute);
}

Expand Down Expand Up @@ -470,13 +470,13 @@ protected function passesOptionalCheck($attribute)
/**
* Determine if the attribute fails the nullable check.
*
* @param string $rule
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function isNotNullIfMarkedAsNullable($attribute, $value)
protected function isNotNullIfMarkedAsNullable($rule, $attribute)
{
if (! $this->hasRule($attribute, ['Nullable'])) {
if (in_array($rule, $this->implicitRules) || ! $this->hasRule($attribute, ['Nullable'])) {
return true;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,37 @@ public function testNullable()
$this->assertEquals('validation.boolean', $v->messages()->get('b')[0]);
}

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

$v = new Validator($trans, [
'x' => null, 'y' => null,
], [
'x' => 'nullable|required_with:y|integer',
'y' => 'nullable|required_with:x|integer',
]);
$this->assertTrue($v->passes());

$v = new Validator($trans, [
'x' => 'value', 'y' => null,
], [
'x' => 'nullable|required_with:y|integer',
'y' => 'nullable|required_with:x|integer',
]);
$this->assertTrue($v->fails());
$this->assertEquals('validation.integer', $v->messages()->get('x')[0]);

$v = new Validator($trans, [
'x' => 123, 'y' => null,
], [
'x' => 'nullable|required_with:y|integer',
'y' => 'nullable|required_with:x|integer',
]);
$this->assertTrue($v->fails());
$this->assertEquals('validation.required_with', $v->messages()->get('y')[0]);
}

public function testProperLanguageLineIsSet()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down

0 comments on commit d8ba110

Please sign in to comment.