Skip to content

Commit

Permalink
Revert "Revert "[6.x] Fix required_if boolean validation"" (#36969)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Apr 14, 2021
1 parent d94c07d commit 15b6358
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1475,13 +1475,24 @@ protected function prepareValuesAndOther($parameters)

$values = array_slice($parameters, 1);

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

return [$values, $other];
}

/**
* Check if parameter should be converted to boolean.
*
* @param string $parameter
* @return bool
*/
protected function shouldConvertToBoolean($parameter)
{
return in_array('boolean', Arr::get($this->rules, $parameter, []));
}

/**
* Convert the given values to boolean if they are string "true" / "false".
*
Expand Down
11 changes: 11 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,17 @@ public function testRequiredIf()
$v = new Validator($trans, ['first' => 'dayle', 'last' => ''], ['last' => 'RequiredIf:first,taylor,dayle']);
$this->assertFalse($v->passes());
$this->assertSame('The last field is required when first is dayle.', $v->messages()->first('last'));

$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
$v = new Validator($trans, ['foo' => 0], [
'foo' => 'required|boolean',
'bar' => 'required_if:foo,true',
'baz' => 'required_if:foo,false',
]);
$this->assertTrue($v->fails());
$this->assertCount(1, $v->messages());
$this->assertSame('The baz field is required when foo is 0.', $v->messages()->first('baz'));
}

public function testRequiredUnless()
Expand Down

0 comments on commit 15b6358

Please sign in to comment.