Skip to content

Commit

Permalink
[10.x] Fix validation message used for max file size (#49879)
Browse files Browse the repository at this point in the history
* Use correct validation message

* Add tests

* Apply styleci fixes

* Update src/Illuminate/Validation/Concerns/FormatsMessages.php

* Use symfony file to get attribute type

* Apply fixes from styleci

---------

Co-authored-by: Dries Vints <[email protected]>
  • Loading branch information
mateusjunges and driesvints authored Jan 29, 2024
1 parent fb9d508 commit d410d61
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
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

0 comments on commit d410d61

Please sign in to comment.