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

[9.x] Allow specifiying custom messages for Rule objects #41145

Merged
merged 1 commit into from
Feb 21, 2022
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
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ protected function validateUsingCustomRule($attribute, $value, $rule)
if (! $rule->passes($attribute, $value)) {
$this->failedRules[$attribute][get_class($rule)] = [];

$messages = $rule->message();
$messages = $this->getFromLocalArray($attribute, get_class($rule)) ?? $rule->message();

$messages = $messages ? (array) $messages : [get_class($rule)];

Expand Down
41 changes: 41 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,47 @@ public function testInlineValidationMessagesAreRespectedWithAsterisks()
$this->assertSame('all must be required!', $v->messages()->first('name.1'));
}

public function testInlineValidationMessagesForRuleObjectsAreRespected()
{
$rule = new class implements Rule
{
public function passes($attribute, $value)
{
return false;
}

public function message()
{
return 'this is my message';
}
};

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['name' => 'Taylor'], ['name' => $rule], [$rule::class => 'my custom message']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertSame('my custom message', $v->messages()->first('name'));

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['name' => 'Ryan'], ['name' => $rule], ['name.' . $rule::class => 'my custom message']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertSame('my custom message', $v->messages()->first('name'));

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['name' => ['foo', 'bar']], ['name.*' => $rule], ['name.*.' . $rule::class => 'my custom message']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertSame('my custom message', $v->messages()->first('name.0'));
$this->assertSame('my custom message', $v->messages()->first('name.1'));

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['name' => 'Ryan'], ['name' => $rule], [$rule::class => 'my attribute is :attribute']);
$this->assertFalse($v->passes());
$v->messages()->setFormat(':message');
$this->assertSame('my attribute is name', $v->messages()->first('name'));
}

public function testIfRulesAreSuccessfullyAdded()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down