Skip to content

Commit

Permalink
[6.x] Fix validator treating null as true for (required|exclude)_(if|…
Browse files Browse the repository at this point in the history
…unless) due to loose in_array() check (#36504)

* Fix required_if treating true as null

* Fix required_unless treating true as null

* Fix exclude_if treating true as null

* Fix exclude_unless treating true as null
  • Loading branch information
jessarcher committed Mar 8, 2021
1 parent c5fa693 commit 3f5af8d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ public function validateRequiredIf($attribute, $value, $parameters)

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

if (in_array($other, $values)) {
if (in_array($other, $values, is_bool($other))) {
return $this->validateRequired($attribute, $value);
}

Expand All @@ -1443,7 +1443,7 @@ public function validateExcludeIf($attribute, $value, $parameters)

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

return ! in_array($other, $values);
return ! in_array($other, $values, is_bool($other));
}

/**
Expand All @@ -1460,7 +1460,7 @@ public function validateExcludeUnless($attribute, $value, $parameters)

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

return in_array($other, $values);
return in_array($other, $values, is_bool($other));
}

/**
Expand Down Expand Up @@ -1515,7 +1515,7 @@ public function validateRequiredUnless($attribute, $value, $parameters)

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

if (! in_array($other, $values)) {
if (! in_array($other, $values, is_bool($other))) {
return $this->validateRequired($attribute, $value);
}

Expand Down
66 changes: 66 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1056,10 +1056,34 @@ public function testRequiredIf()
$v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,false']);
$this->assertTrue($v->passes());

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

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

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

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

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

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

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

// error message when passed multiple values (required_if:foo,bar,baz)
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
Expand Down Expand Up @@ -1098,6 +1122,26 @@ 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, ['foo' => true], ['bar' => 'required_unless:foo,null']);
$this->assertTrue($v->fails());

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

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

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

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

// error message when passed multiple values (required_unless:foo,bar,baz)
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.required_unless' => 'The :attribute field is required unless :other is in :values.'], 'en');
Expand Down Expand Up @@ -5074,6 +5118,20 @@ public function providesPassingExcludeIfData()
'has_appointment' => false,
],
],
[
[
'has_appointment' => ['nullable', 'bool'],
'appointment_date' => ['exclude_if:has_appointment,null', 'required', 'date'],
],
[
'has_appointment' => true,
'appointment_date' => '2021-03-08',
],
[
'has_appointment' => true,
'appointment_date' => '2021-03-08',
],
],
[
[
'has_appointment' => ['required', 'bool'],
Expand Down Expand Up @@ -5408,6 +5466,14 @@ public function testExcludeUnless()
);
$this->assertTrue($validator->fails());
$this->assertSame(['mouse' => ['validation.required']], $validator->messages()->toArray());

$validator = new Validator(
$this->getIlluminateArrayTranslator(),
['foo' => true, 'bar' => 'baz'],
['foo' => 'nullable', 'bar' => 'exclude_unless:foo,null']
);
$this->assertTrue($validator->passes());
$this->assertSame(['foo' => true], $validator->validated());
}

public function testExcludeValuesAreReallyRemoved()
Expand Down

0 comments on commit 3f5af8d

Please sign in to comment.