|
7 | 7 | use Illuminate\Contracts\Validation\ValidatorAwareRule;
|
8 | 8 | use Illuminate\Translation\ArrayLoader;
|
9 | 9 | use Illuminate\Translation\Translator;
|
| 10 | +use Illuminate\Validation\InvokableValidationRule; |
10 | 11 | use Illuminate\Validation\Validator;
|
11 | 12 | use PHPUnit\Framework\TestCase;
|
12 | 13 |
|
@@ -333,6 +334,85 @@ public function __invoke($attribute, $value, $fail)
|
333 | 334 | ], $validator->messages()->messages());
|
334 | 335 | }
|
335 | 336 |
|
| 337 | + public function testExplicitRuleCanUseInlineValidationMessages() |
| 338 | + { |
| 339 | + $trans = $this->getIlluminateArrayTranslator(); |
| 340 | + $rule = new class() implements InvokableRule |
| 341 | + { |
| 342 | + public $implicit = false; |
| 343 | + |
| 344 | + public function __invoke($attribute, $value, $fail) |
| 345 | + { |
| 346 | + $fail('xxxx'); |
| 347 | + } |
| 348 | + }; |
| 349 | + |
| 350 | + $validator = new Validator($trans, ['foo' => 'bar'], ['foo' => $rule], [$rule::class => ':attribute custom.']); |
| 351 | + |
| 352 | + $this->assertFalse($validator->passes()); |
| 353 | + $this->assertSame([ |
| 354 | + 'foo' => [ |
| 355 | + 'foo custom.', |
| 356 | + ], |
| 357 | + ], $validator->messages()->messages()); |
| 358 | + |
| 359 | + $validator = new Validator($trans, ['foo' => 'bar'], ['foo' => $rule], ['foo.'.$rule::class => ':attribute custom with key.']); |
| 360 | + |
| 361 | + $this->assertFalse($validator->passes()); |
| 362 | + $this->assertSame([ |
| 363 | + 'foo' => [ |
| 364 | + 'foo custom with key.', |
| 365 | + ], |
| 366 | + ], $validator->messages()->messages()); |
| 367 | + } |
| 368 | + |
| 369 | + public function testImplicitRuleCanUseInlineValidationMessages() |
| 370 | + { |
| 371 | + $trans = $this->getIlluminateArrayTranslator(); |
| 372 | + $rule = new class() implements InvokableRule |
| 373 | + { |
| 374 | + public $implicit = true; |
| 375 | + |
| 376 | + public function __invoke($attribute, $value, $fail) |
| 377 | + { |
| 378 | + $fail('xxxx'); |
| 379 | + } |
| 380 | + }; |
| 381 | + |
| 382 | + $validator = new Validator($trans, ['foo' => ''], ['foo' => $rule], [$rule::class => ':attribute custom.']); |
| 383 | + |
| 384 | + $this->assertFalse($validator->passes()); |
| 385 | + $this->assertSame([ |
| 386 | + 'foo' => [ |
| 387 | + 'foo custom.', |
| 388 | + ], |
| 389 | + ], $validator->messages()->messages()); |
| 390 | + |
| 391 | + $validator = new Validator($trans, ['foo' => ''], ['foo' => $rule], ['foo.'.$rule::class => ':attribute custom with key.']); |
| 392 | + |
| 393 | + $this->assertFalse($validator->passes()); |
| 394 | + $this->assertSame([ |
| 395 | + 'foo' => [ |
| 396 | + 'foo custom with key.', |
| 397 | + ], |
| 398 | + ], $validator->messages()->messages()); |
| 399 | + } |
| 400 | + |
| 401 | + public function testItCanReturnInvokableRule() |
| 402 | + { |
| 403 | + $rule = new class() implements InvokableRule |
| 404 | + { |
| 405 | + public function __invoke($attribute, $value, $fail) |
| 406 | + { |
| 407 | + $fail('xxxx'); |
| 408 | + } |
| 409 | + }; |
| 410 | + |
| 411 | + $invokableValidationRule = InvokableValidationRule::make($rule); |
| 412 | + |
| 413 | + $this->assertSame($rule, $invokableValidationRule->invokable()); |
| 414 | + } |
| 415 | + |
336 | 416 | private function getIlluminateArrayTranslator()
|
337 | 417 | {
|
338 | 418 | return new Translator(
|
|
0 commit comments