Skip to content

Commit

Permalink
fix required if with bools
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Apr 27, 2016
1 parent 0a26661 commit 105fdb2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,13 @@ protected function validateRequiredIf($attribute, $value, $parameters)

$values = array_slice($parameters, 1);

if (is_bool($data)) {
array_walk($values, function (&$value) {
$value = $value == 'true' ?
true : ($value == 'false' ? false : $value);
});
}

if (in_array($data, $values)) {
return $this->validateRequired($attribute, $value);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,14 @@ public function testRequiredIf()
$v = new Validator($trans, ['first' => 'dayle', 'last' => 'rees'], ['last' => 'required_if:first,taylor,dayle']);
$this->assertTrue($v->passes());

$trans = $this->getRealTranslator();
$v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,false']);
$this->assertTrue($v->passes());

$trans = $this->getRealTranslator();
$v = new Validator($trans, ['foo' => true], ['bar' => 'required_if:foo,true']);
$this->assertTrue($v->fails());

// error message when passed multiple values (required_if:foo,bar,baz)
$trans = $this->getRealTranslator();
$trans->addResource('array', ['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en', 'messages');
Expand Down

0 comments on commit 105fdb2

Please sign in to comment.