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 required_if boolean validation #36969

Merged
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
13 changes: 12 additions & 1 deletion src/Illuminate/Validation/Concerns/ValidatesAttributes.php
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
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