Skip to content

Commit 3f5af8d

Browse files
authored
[6.x] Fix validator treating null as true for (required|exclude)_(if|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
1 parent c5fa693 commit 3f5af8d

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-4
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ public function validateRequiredIf($attribute, $value, $parameters)
14221422

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

1425-
if (in_array($other, $values)) {
1425+
if (in_array($other, $values, is_bool($other))) {
14261426
return $this->validateRequired($attribute, $value);
14271427
}
14281428

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

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

1446-
return ! in_array($other, $values);
1446+
return ! in_array($other, $values, is_bool($other));
14471447
}
14481448

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

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

1463-
return in_array($other, $values);
1463+
return in_array($other, $values, is_bool($other));
14641464
}
14651465

14661466
/**
@@ -1515,7 +1515,7 @@ public function validateRequiredUnless($attribute, $value, $parameters)
15151515

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

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

tests/Validation/ValidationValidatorTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,10 +1056,34 @@ public function testRequiredIf()
10561056
$v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,false']);
10571057
$this->assertTrue($v->passes());
10581058

1059+
$trans = $this->getIlluminateArrayTranslator();
1060+
$v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,null']);
1061+
$this->assertTrue($v->passes());
1062+
1063+
$trans = $this->getIlluminateArrayTranslator();
1064+
$v = new Validator($trans, ['foo' => 0], ['bar' => 'required_if:foo,0']);
1065+
$this->assertTrue($v->fails());
1066+
1067+
$trans = $this->getIlluminateArrayTranslator();
1068+
$v = new Validator($trans, ['foo' => '0'], ['bar' => 'required_if:foo,0']);
1069+
$this->assertTrue($v->fails());
1070+
1071+
$trans = $this->getIlluminateArrayTranslator();
1072+
$v = new Validator($trans, ['foo' => 1], ['bar' => 'required_if:foo,1']);
1073+
$this->assertTrue($v->fails());
1074+
1075+
$trans = $this->getIlluminateArrayTranslator();
1076+
$v = new Validator($trans, ['foo' => '1'], ['bar' => 'required_if:foo,1']);
1077+
$this->assertTrue($v->fails());
1078+
10591079
$trans = $this->getIlluminateArrayTranslator();
10601080
$v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,true']);
10611081
$this->assertTrue($v->fails());
10621082

1083+
$trans = $this->getIlluminateArrayTranslator();
1084+
$v = new Validator($trans, ['foo' => false], ['bar' => 'required_if:foo,false']);
1085+
$this->assertTrue($v->fails());
1086+
10631087
// error message when passed multiple values (required_if:foo,bar,baz)
10641088
$trans = $this->getIlluminateArrayTranslator();
10651089
$trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
@@ -1098,6 +1122,26 @@ public function testRequiredUnless()
10981122
$v = new Validator($trans, ['foo' => false], ['bar' => 'required_unless:foo,true']);
10991123
$this->assertTrue($v->fails());
11001124

1125+
$trans = $this->getIlluminateArrayTranslator();
1126+
$v = new Validator($trans, ['foo' => true], ['bar' => 'required_unless:foo,null']);
1127+
$this->assertTrue($v->fails());
1128+
1129+
$trans = $this->getIlluminateArrayTranslator();
1130+
$v = new Validator($trans, ['foo' => '0'], ['bar' => 'required_unless:foo,0']);
1131+
$this->assertTrue($v->passes());
1132+
1133+
$trans = $this->getIlluminateArrayTranslator();
1134+
$v = new Validator($trans, ['foo' => 0], ['bar' => 'required_unless:foo,0']);
1135+
$this->assertTrue($v->passes());
1136+
1137+
$trans = $this->getIlluminateArrayTranslator();
1138+
$v = new Validator($trans, ['foo' => '1'], ['bar' => 'required_unless:foo,1']);
1139+
$this->assertTrue($v->passes());
1140+
1141+
$trans = $this->getIlluminateArrayTranslator();
1142+
$v = new Validator($trans, ['foo' => 1], ['bar' => 'required_unless:foo,1']);
1143+
$this->assertTrue($v->passes());
1144+
11011145
// error message when passed multiple values (required_unless:foo,bar,baz)
11021146
$trans = $this->getIlluminateArrayTranslator();
11031147
$trans->addLines(['validation.required_unless' => 'The :attribute field is required unless :other is in :values.'], 'en');
@@ -5074,6 +5118,20 @@ public function providesPassingExcludeIfData()
50745118
'has_appointment' => false,
50755119
],
50765120
],
5121+
[
5122+
[
5123+
'has_appointment' => ['nullable', 'bool'],
5124+
'appointment_date' => ['exclude_if:has_appointment,null', 'required', 'date'],
5125+
],
5126+
[
5127+
'has_appointment' => true,
5128+
'appointment_date' => '2021-03-08',
5129+
],
5130+
[
5131+
'has_appointment' => true,
5132+
'appointment_date' => '2021-03-08',
5133+
],
5134+
],
50775135
[
50785136
[
50795137
'has_appointment' => ['required', 'bool'],
@@ -5408,6 +5466,14 @@ public function testExcludeUnless()
54085466
);
54095467
$this->assertTrue($validator->fails());
54105468
$this->assertSame(['mouse' => ['validation.required']], $validator->messages()->toArray());
5469+
5470+
$validator = new Validator(
5471+
$this->getIlluminateArrayTranslator(),
5472+
['foo' => true, 'bar' => 'baz'],
5473+
['foo' => 'nullable', 'bar' => 'exclude_unless:foo,null']
5474+
);
5475+
$this->assertTrue($validator->passes());
5476+
$this->assertSame(['foo' => true], $validator->validated());
54115477
}
54125478

54135479
public function testExcludeValuesAreReallyRemoved()

0 commit comments

Comments
 (0)