Skip to content

Commit

Permalink
Add displayable value to required_unless
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagorb committed Apr 8, 2018
1 parent b4b89ad commit 39cbd00
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,14 @@ protected function replaceRequiredIf($message, $attribute, $rule, $parameters)
*/
protected function replaceRequiredUnless($message, $attribute, $rule, $parameters)
{
$other = $this->getDisplayableAttribute(array_shift($parameters));
$other = $this->getDisplayableAttribute($parameters[0]);

return str_replace([':other', ':values'], [$other, implode(', ', $parameters)], $message);
$values = [];
foreach (array_slice($parameters, 1) as $value) {
$values[] = $this->getDisplayableValue($parameters[0], $value);
}

return str_replace([':other', ':values'], [$other, implode(', ', $values)], $message);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@ public function testDisplayableValuesAreReplaced()
$v->messages()->setFormat(':message');
$this->assertEquals('The bar field is required when color is red.', $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');
$trans->addLines(['validation.values.color.1' => 'red'], 'en');
$v = new Validator($trans, ['color' => '2', 'bar' => ''], ['bar' => 'RequiredUnless:color,1']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertEquals('The bar field is required unless color is in red.', $v->messages()->first('bar'));

//in:foo,bar,...
$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.in' => ':attribute must be included in :values.'], 'en');
Expand Down

0 comments on commit 39cbd00

Please sign in to comment.