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
17 changes: 8 additions & 9 deletions src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;

trait FormatsMessages
Expand Down Expand Up @@ -218,15 +219,13 @@ protected function getAttributeType($attribute)
// We assume that the attributes present in the file array are files so that
// means that if the attribute does not have a numeric rule and the files
// list doesn't have it we'll just consider it a string by elimination.
if ($this->hasRule($attribute, $this->numericRules)) {
return 'numeric';
} elseif ($this->hasRule($attribute, ['Array'])) {
return 'array';
} elseif ($this->getValue($attribute) instanceof UploadedFile) {
return 'file';
}

return 'string';
return match (true) {
$this->hasRule($attribute, $this->numericRules) => 'numeric',
$this->hasRule($attribute, ['Array']) => 'array',
$this->getValue($attribute) instanceof UploadedFile,
$this->getValue($attribute) instanceof File => 'file',
default => 'string',
};
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Validation/ValidationFileRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ public function testMacro()
);
}

public function testItUsesTheCorrectValidationMessageForFile(): void
{
file_put_contents($path = __DIR__.'/test.json', 'this-is-a-test');

$file = new \Illuminate\Http\File($path);

$this->fails(
['max:0'],
$file,
['validation.max.file']
);

unlink($path);
}

public function testItCanSetDefaultUsing()
{
$this->assertInstanceOf(File::class, File::default());
Expand Down