Skip to content

Commit

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

This reverts commit 3f5af8d.
  • Loading branch information
taylorotwell committed Mar 11, 2021
1 parent bbb6b97 commit 2f7552f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 70 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, is_bool($other))) {
if (in_array($other, $values)) {
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, is_bool($other));
return ! in_array($other, $values);
}

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

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

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

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

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

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

Expand Down
66 changes: 0 additions & 66 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1056,34 +1056,10 @@ 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 @@ -1122,26 +1098,6 @@ 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 @@ -5118,20 +5074,6 @@ 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 @@ -5466,14 +5408,6 @@ 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 2f7552f

Please sign in to comment.