Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Fix validation message used for max file size #49879

Merged
merged 9 commits into from
Jan 29, 2024
17 changes: 8 additions & 9 deletions src/Illuminate/Validation/Concerns/FormatsMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Validation\Concerns;

use Closure;
use Illuminate\Http\File;
mateusjunges marked this conversation as resolved.
Show resolved Hide resolved
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\File\UploadedFile;
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
Loading