Skip to content
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
6 changes: 6 additions & 0 deletions src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 26 additions & 0 deletions tests/Validation/ValidationAnyOfRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions tests/Validation/ValidationEnumRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions tests/Validation/ValidationRuleCanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down