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 invokable rules to specify custom messsages #43925

Merged
merged 3 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions src/Illuminate/Validation/InvokableValidationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ public function message()
return $this->messages;
}

/**
* Get the invokable.
*
* @return \Illuminate\Contracts\Validation\InvokableRule
*/
public function invokable()
{
return $this->invokable;
}

/**
* Set the data under validation.
*
Expand Down
12 changes: 8 additions & 4 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,17 +810,21 @@ protected function validateUsingCustomRule($attribute, $value, $rule)
}

if (! $rule->passes($attribute, $value)) {
$this->failedRules[$attribute][get_class($rule)] = [];
$ruleClass = $rule instanceof InvokableValidationRule ?
get_class($rule->invokable()) :
get_class($rule);

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

$messages = $messages ? (array) $messages : [get_class($rule)];
$messages = $this->getFromLocalArray($attribute, $ruleClass) ?? $rule->message();

$messages = $messages ? (array) $messages : [$ruleClass];

foreach ($messages as $key => $message) {
$key = is_string($key) ? $key : $attribute;

$this->messages->add($key, $this->makeReplacements(
$message, $key, get_class($rule), []
$message, $key, $ruleClass, []
));
}
}
Expand Down
80 changes: 80 additions & 0 deletions tests/Validation/ValidationInvokableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;
use Illuminate\Validation\InvokableValidationRule;
use Illuminate\Validation\Validator;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -333,6 +334,85 @@ public function __invoke($attribute, $value, $fail)
], $validator->messages()->messages());
}

public function testExplicitRuleCanUseInlineValidationMessages()
{
$trans = $this->getIlluminateArrayTranslator();
$rule = new class() implements InvokableRule
{
public $implicit = false;

public function __invoke($attribute, $value, $fail)
{
$fail('xxxx');
}
};

$validator = new Validator($trans, ['foo' => 'bar'], ['foo' => $rule], [$rule::class => ':attribute custom.']);

$this->assertFalse($validator->passes());
$this->assertSame([
'foo' => [
'foo custom.',
],
], $validator->messages()->messages());

$validator = new Validator($trans, ['foo' => 'bar'], ['foo' => $rule], ['foo.'.$rule::class => ':attribute custom with key.']);

$this->assertFalse($validator->passes());
$this->assertSame([
'foo' => [
'foo custom with key.',
],
], $validator->messages()->messages());
}

public function testImplicitRuleCanUseInlineValidationMessages()
{
$trans = $this->getIlluminateArrayTranslator();
$rule = new class() implements InvokableRule
{
public $implicit = true;

public function __invoke($attribute, $value, $fail)
{
$fail('xxxx');
}
};

$validator = new Validator($trans, ['foo' => ''], ['foo' => $rule], [$rule::class => ':attribute custom.']);

$this->assertFalse($validator->passes());
$this->assertSame([
'foo' => [
'foo custom.',
],
], $validator->messages()->messages());

$validator = new Validator($trans, ['foo' => ''], ['foo' => $rule], ['foo.'.$rule::class => ':attribute custom with key.']);

$this->assertFalse($validator->passes());
$this->assertSame([
'foo' => [
'foo custom with key.',
],
], $validator->messages()->messages());
}

public function testItCanReturnInvokableRule()
{
$rule = new class() implements InvokableRule
{
public function __invoke($attribute, $value, $fail)
{
$fail('xxxx');
}
};

$invokableValidationRule = InvokableValidationRule::make($rule);

$this->assertSame($rule, $invokableValidationRule->invokable());
}

private function getIlluminateArrayTranslator()
{
return new Translator(
Expand Down