Skip to content

Commit 15b6358

Browse files
authored
Revert "Revert "[6.x] Fix required_if boolean validation"" (#36969)
1 parent d94c07d commit 15b6358

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1475,13 +1475,24 @@ protected function prepareValuesAndOther($parameters)
14751475

14761476
$values = array_slice($parameters, 1);
14771477

1478-
if (is_bool($other)) {
1478+
if ($this->shouldConvertToBoolean($parameters[0]) || is_bool($other)) {
14791479
$values = $this->convertValuesToBoolean($values);
14801480
}
14811481

14821482
return [$values, $other];
14831483
}
14841484

1485+
/**
1486+
* Check if parameter should be converted to boolean.
1487+
*
1488+
* @param string $parameter
1489+
* @return bool
1490+
*/
1491+
protected function shouldConvertToBoolean($parameter)
1492+
{
1493+
return in_array('boolean', Arr::get($this->rules, $parameter, []));
1494+
}
1495+
14851496
/**
14861497
* Convert the given values to boolean if they are string "true" / "false".
14871498
*

tests/Validation/ValidationValidatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,17 @@ public function testRequiredIf()
10901090
$v = new Validator($trans, ['first' => 'dayle', 'last' => ''], ['last' => 'RequiredIf:first,taylor,dayle']);
10911091
$this->assertFalse($v->passes());
10921092
$this->assertSame('The last field is required when first is dayle.', $v->messages()->first('last'));
1093+
1094+
$trans = $this->getIlluminateArrayTranslator();
1095+
$trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
1096+
$v = new Validator($trans, ['foo' => 0], [
1097+
'foo' => 'required|boolean',
1098+
'bar' => 'required_if:foo,true',
1099+
'baz' => 'required_if:foo,false',
1100+
]);
1101+
$this->assertTrue($v->fails());
1102+
$this->assertCount(1, $v->messages());
1103+
$this->assertSame('The baz field is required when foo is 0.', $v->messages()->first('baz'));
10931104
}
10941105

10951106
public function testRequiredUnless()

0 commit comments

Comments
 (0)