Skip to content

Commit

Permalink
Merge pull request #29560 from codinglabsau/boolean-displayable-value
Browse files Browse the repository at this point in the history
Fix non-displayable boolean values in validation messages
  • Loading branch information
taylorotwell committed Aug 14, 2019
2 parents baca22d + 16b4f41 commit 3069091
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ public function getDisplayableValue($attribute, $value)
return $line;
}

if (is_bool($value)) {
return $value ? 'true' : 'false';
}

return $value;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,23 @@ public function testDisplayableValuesAreReplaced()
$v->messages()->setFormat(':message');
$this->assertEquals('The bar field is required when color is red.', $v->messages()->first('bar'));

//required_if:foo,boolean
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
$trans->addLines(['validation.values.subscribe.false' => 'false'], 'en');
$v = new Validator($trans, ['subscribe' => false, 'bar' => ''], ['bar' => 'RequiredIf:subscribe,false']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('The bar field is required when subscribe is false.', $v->messages()->first('bar'));

$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.required_if' => 'The :attribute field is required when :other is :value.'], 'en');
$trans->addLines(['validation.values.subscribe.true' => 'true'], 'en');
$v = new Validator($trans, ['subscribe' => true, 'bar' => ''], ['bar' => 'RequiredIf:subscribe,true']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('The bar field is required when subscribe is true.', $v->messages()->first('bar'));

//required_unless:foo,bar
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.required_unless' => 'The :attribute field is required unless :other is in :values.'], 'en');
Expand Down

0 comments on commit 3069091

Please sign in to comment.