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

[5.6] Add displayable value to required_unless #23833

Merged
merged 1 commit into from
Apr 9, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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