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

[6.x] Fix validator treating null as true for (required|exclude)_(if|unless) due to loose in_array() check #36504

Merged
merged 4 commits into from
Mar 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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