diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 6b1ba8f24195..9fac13cb8298 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -103,6 +103,12 @@ protected function getFromLocalArray($attribute, $lowerRule, $source = null) $keys = ["{$attribute}.{$lowerRule}", $lowerRule, $attribute]; + $shortRule = "{$attribute}.".Str::snake(class_basename($lowerRule)); + + if (! in_array($shortRule, $keys)) { + $keys[] = $shortRule; + } + // First we will check for a custom message for an attribute specific rule // message for the fields, then we will check for a general custom line // that is not attribute specific. If we find either we'll return it. diff --git a/tests/Validation/ValidationAnyOfRuleTest.php b/tests/Validation/ValidationAnyOfRuleTest.php index f0806182c693..ee9474a707a6 100644 --- a/tests/Validation/ValidationAnyOfRuleTest.php +++ b/tests/Validation/ValidationAnyOfRuleTest.php @@ -349,6 +349,32 @@ protected function setUpRuleSets() ]; } + public function testCustomMessageUsingDotNotationAndFqcnWorks() + { + $v = new Validator( + resolve('translator'), + [ + 'string' => 123, + 'string_fqcn' => 456, + ], + [ + 'string' => Rule::anyOf(['string']), + 'string_fqcn' => Rule::anyOf(['string']), + ], + [ + 'string.any_of' => 'Please choose a valid string (dot notation)', + 'string_fqcn.Illuminate\Validation\Rules\AnyOf' => 'Please choose a valid string (fqcn)', + ] + ); + + $this->assertTrue($v->fails()); + + $this->assertSame([ + 'Please choose a valid string (dot notation)', + 'Please choose a valid string (fqcn)', + ], $v->messages()->all()); + } + protected function setUp(): void { parent::setUp(); diff --git a/tests/Validation/ValidationEnumRuleTest.php b/tests/Validation/ValidationEnumRuleTest.php index bf9ec319dc66..a0399805def4 100644 --- a/tests/Validation/ValidationEnumRuleTest.php +++ b/tests/Validation/ValidationEnumRuleTest.php @@ -277,6 +277,32 @@ public static function conditionalCasesDataProvider(): array ]; } + public function testCustomMessageUsingDotNotationAndFqcnWorks() + { + $v = new Validator( + resolve('translator'), + [ + 'status' => 'invalid_value', + 'status_fqcn' => 'another_invalid', + ], + [ + 'status' => new Enum(StringStatus::class), + 'status_fqcn' => new Enum(StringStatus::class), + ], + [ + 'status.enum' => 'Please choose a valid status (dot notation)', + 'status_fqcn.Illuminate\Validation\Rules\Enum' => 'Please choose a valid status (fqcn)', + ] + ); + + $this->assertTrue($v->fails()); + + $this->assertSame([ + 'Please choose a valid status (dot notation)', + 'Please choose a valid status (fqcn)', + ], $v->messages()->all()); + } + protected function setUp(): void { $container = Container::getInstance(); diff --git a/tests/Validation/ValidationRuleCanTest.php b/tests/Validation/ValidationRuleCanTest.php index 8b7f7c92173d..71275bf65873 100644 --- a/tests/Validation/ValidationRuleCanTest.php +++ b/tests/Validation/ValidationRuleCanTest.php @@ -90,6 +90,32 @@ public function testValidationPasses() $this->assertTrue($v->passes()); } + public function testCustomMessageUsingDotNotationAndFqcnWorks() + { + $v = new Validator( + resolve('translator'), + [ + 'company' => '1', + 'company_fqcn' => '1', + ], + [ + 'company' => new Can('update-company', [\App\Models\Company::class, new stdClass]), + 'company_fqcn' => new Can('update-company', [\App\Models\Company::class, new stdClass]), + ], + [ + 'company.can' => 'You dont have permission (dot notation)', + 'company_fqcn.Illuminate\Validation\Rules\Can' => 'You dont have permission (fqcn)', + ] + ); + + $this->assertTrue($v->fails()); + + $this->assertSame([ + 'You dont have permission (dot notation)', + 'You dont have permission (fqcn)', + ], $v->messages()->all()); + } + /** * Get the Gate instance from the container. *