From 69ee952de89105e96fd7d337351d54b33e5dea5c Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Sun, 28 Jan 2024 03:04:06 -0300 Subject: [PATCH 1/6] Use correct validation message --- .../Validation/Concerns/FormatsMessages.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 4d4042a4e10f..719030f230b1 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -3,6 +3,7 @@ namespace Illuminate\Validation\Concerns; use Closure; +use Illuminate\Http\File; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\File\UploadedFile; @@ -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 => 'file', + $this->getValue($attribute) instanceof File => 'file', + default => 'string', + }; } /** From bcfc65a7280eb7bfbdc65064b8b4316807de2adf Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Sun, 28 Jan 2024 03:04:12 -0300 Subject: [PATCH 2/6] Add tests --- tests/Validation/ValidationFileRuleTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Validation/ValidationFileRuleTest.php b/tests/Validation/ValidationFileRuleTest.php index 838aa6615471..1705a3ef198b 100644 --- a/tests/Validation/ValidationFileRuleTest.php +++ b/tests/Validation/ValidationFileRuleTest.php @@ -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()); From 599e60cc134a18c7287dbb197e9cd2c92e0e4bd1 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Sun, 28 Jan 2024 03:12:39 -0300 Subject: [PATCH 3/6] Apply styleci fixes --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 719030f230b1..e0dc6b2fe45e 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -219,7 +219,7 @@ 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. - return match(true) { + return match (true) { $this->hasRule($attribute, $this->numericRules) => 'numeric', $this->hasRule($attribute, ['Array']) => 'array', $this->getValue($attribute) instanceof UploadedFile => 'file', From 7c4aa2b6eab2e3683e18c732a805329f72b7814b Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 29 Jan 2024 10:27:21 +0100 Subject: [PATCH 4/6] Update src/Illuminate/Validation/Concerns/FormatsMessages.php --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index e0dc6b2fe45e..0ec6cc680dd7 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -222,7 +222,7 @@ protected function getAttributeType($attribute) return match (true) { $this->hasRule($attribute, $this->numericRules) => 'numeric', $this->hasRule($attribute, ['Array']) => 'array', - $this->getValue($attribute) instanceof UploadedFile => 'file', + $this->getValue($attribute) instanceof UploadedFile, $this->getValue($attribute) instanceof File => 'file', default => 'string', }; From 9d741065a81668db13d43b256c7f76ed916f48ea Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Mon, 29 Jan 2024 10:24:48 -0300 Subject: [PATCH 5/6] Use symfony file to get attribute type --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 0ec6cc680dd7..2bcc379be1e0 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -3,7 +3,7 @@ namespace Illuminate\Validation\Concerns; use Closure; -use Illuminate\Http\File; +use Symfony\Component\HttpFoundation\File\File; use Illuminate\Support\Arr; use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\File\UploadedFile; From 1ba0acbac3048e6329a5d95a2e8c640b80b5ca11 Mon Sep 17 00:00:00 2001 From: Mateus Junges Date: Mon, 29 Jan 2024 10:28:46 -0300 Subject: [PATCH 6/6] Apply fixes from styleci --- src/Illuminate/Validation/Concerns/FormatsMessages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Concerns/FormatsMessages.php b/src/Illuminate/Validation/Concerns/FormatsMessages.php index 2bcc379be1e0..2d68828d1b3a 100644 --- a/src/Illuminate/Validation/Concerns/FormatsMessages.php +++ b/src/Illuminate/Validation/Concerns/FormatsMessages.php @@ -3,9 +3,9 @@ namespace Illuminate\Validation\Concerns; use Closure; -use Symfony\Component\HttpFoundation\File\File; use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\UploadedFile; trait FormatsMessages