Skip to content

Commit

Permalink
Fix unless rules (#37291)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored May 6, 2021
1 parent eaea47a commit 325699a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
8 changes: 0 additions & 8 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1466,10 +1466,6 @@ public function validateExcludeUnless($attribute, $value, $parameters)
{
$this->requireParameterCount(2, $parameters, 'exclude_unless');

if (! Arr::has($this->data, $parameters[0])) {
return true;
}

[$values, $other] = $this->prepareValuesAndOther($parameters);

return in_array($other, $values, is_bool($other) || is_null($other));
Expand All @@ -1487,10 +1483,6 @@ public function validateRequiredUnless($attribute, $value, $parameters)
{
$this->requireParameterCount(2, $parameters, 'required_unless');

if (! Arr::has($this->data, $parameters[0])) {
return true;
}

[$values, $other] = $this->prepareValuesAndOther($parameters);

if (! in_array($other, $values, is_bool($other) || is_null($other))) {
Expand Down
26 changes: 26 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,10 @@ public function testRequiredUnless()
$v = new Validator($trans, ['first' => 'sven'], ['last' => 'required_unless:first,taylor,sven']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, [], ['bar' => 'required_unless:foo,true']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => false], ['bar' => 'required_unless:foo,false']);
$this->assertTrue($v->passes());
Expand All @@ -1164,6 +1168,18 @@ public function testRequiredUnless()
$v = new Validator($trans, ['foo' => false], ['bar' => 'required_unless:foo,true']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['bar' => '1'], ['bar' => 'required_unless:foo,true']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, [], ['bar' => 'required_unless:foo,true']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, [], ['bar' => 'required_unless:foo,null']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['foo' => true], ['bar' => 'required_unless:foo,null']);
$this->assertTrue($v->fails());
Expand Down Expand Up @@ -5524,6 +5540,16 @@ public function testExcludeUnless()
);
$this->assertTrue($validator->passes());
$this->assertSame(['foo' => true], $validator->validated());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['bar' => 'Hello'], ['bar' => 'exclude_unless:foo,true']);
$this->assertTrue($v->passes());
$this->assertSame([], $v->validated());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['bar' => 'Hello'], ['bar' => 'exclude_unless:foo,null']);
$this->assertTrue($v->passes());
$this->assertSame(['bar' => 'Hello'], $v->validated());
}

public function testExcludeValuesAreReallyRemoved()
Expand Down

0 comments on commit 325699a

Please sign in to comment.