Skip to content

Commit 264ae5b

Browse files
authored
[12.x] Ensure custom validation messages work for the File rule (#57656)
* init * be more Laravel-esque * cs * Update FileValidationTest.php * no need to be strict, be like elsewhere.
1 parent b398db0 commit 264ae5b

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/Illuminate/Validation/Concerns/FormatsMessages.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ protected function getFromLocalArray($attribute, $lowerRule, $source = null)
103103

104104
$keys = ["{$attribute}.{$lowerRule}", $lowerRule, $attribute];
105105

106-
$shortRule = "{$attribute}.".Str::snake(class_basename($lowerRule));
106+
if ($this->getAttributeType($attribute) !== 'file') {
107+
$shortRule = "{$attribute}.".Str::snake(class_basename($lowerRule));
107108

108-
if (! in_array($shortRule, $keys)) {
109-
$keys[] = $shortRule;
109+
if (! in_array($shortRule, $keys)) {
110+
$keys[] = $shortRule;
111+
}
110112
}
111113

112114
// First we will check for a custom message for an attribute specific rule

tests/Integration/Validation/Rules/FileValidationTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,30 @@ public function test_it_can_validate_attribute_as_array_when_validation_should_f
5151
0 => __('validation.mimetypes', ['attribute' => sprintf('files.%s', str_replace('_', ' ', $attribute)), 'values' => implode(', ', $mimes)]),
5252
], $validator->messages()->all());
5353
}
54+
55+
public function test_file_custom_validation_messages()
56+
{
57+
$validator = Validator::make(
58+
[
59+
'one' => UploadedFile::fake()->create('photo', 1000),
60+
'two' => 'not-a-file',
61+
],
62+
[
63+
'one' => [File::default()->max(50)],
64+
'two' => [File::default()->max(50)],
65+
],
66+
[
67+
'one.max' => 'File one is too large',
68+
'one.file' => 'File one is not a file',
69+
'two.max' => 'File two is too large',
70+
'two.file' => 'File two is not a file',
71+
]);
72+
73+
$this->assertTrue($validator->fails());
74+
75+
$this->assertSame([
76+
'File one is too large',
77+
'File two is not a file',
78+
], $validator->messages()->all());
79+
}
5480
}

0 commit comments

Comments
 (0)