diff --git a/src/Forms/Controls/UploadControl.php b/src/Forms/Controls/UploadControl.php index f28da81fa..20008e8d3 100644 --- a/src/Forms/Controls/UploadControl.php +++ b/src/Forms/Controls/UploadControl.php @@ -105,7 +105,7 @@ public function isOk(): bool public function addRule($validator, $errorMessage = null, $arg = null) { if ($validator === Form::Image) { - $this->control->accept = implode(', ', FileUpload::IMAGE_MIME_TYPES); + $this->control->accept = implode(', ', Forms\Helpers::getSupportedImages()); } elseif ($validator === Form::MimeType) { $this->control->accept = implode(', ', (array) $arg); diff --git a/src/Forms/Helpers.php b/src/Forms/Helpers.php index ee39042af..d20eecad7 100644 --- a/src/Forms/Helpers.php +++ b/src/Forms/Helpers.php @@ -293,4 +293,18 @@ public static function getSingleType($reflection): ?string ); } } + + + /** @internal */ + public static function getSupportedImages(): array + { + $flag = imagetypes(); + return array_filter([ + $flag & IMG_GIF ? 'image/gif' : null, + $flag & IMG_JPG ? 'image/jpeg' : null, + $flag & IMG_PNG ? 'image/png' : null, + $flag & IMG_WEBP ? 'image/webp' : null, + $flag & 256 ? 'image/avif' : null, // IMG_AVIF + ]); + } } diff --git a/src/Forms/Rules.php b/src/Forms/Rules.php index c98d249c8..092c3ff91 100644 --- a/src/Forms/Rules.php +++ b/src/Forms/Rules.php @@ -88,9 +88,9 @@ public function addRule($validator, $errorMessage = null, $arg = null) $rule = new Rule; $rule->control = $this->control; $rule->validator = $validator; - $this->adjustOperation($rule); $rule->arg = $arg; $rule->message = $errorMessage; + $this->adjustOperation($rule); if ($rule->validator === Form::Filled) { $this->required = $rule; } else { @@ -339,6 +339,10 @@ private function adjustOperation(Rule $rule): void } } + if ($rule->validator === Form::Image) { + $rule->arg = Helpers::getSupportedImages(); + } + if (!is_callable($this->getCallback($rule))) { $validator = is_scalar($rule->validator) ? " '$rule->validator'" diff --git a/src/assets/netteForms.js b/src/assets/netteForms.js index f9812abbc..1efbac041 100644 --- a/src/assets/netteForms.js +++ b/src/assets/netteForms.js @@ -548,7 +548,7 @@ }, image: function (elem, arg, val) { - return Nette.validators.mimeType(elem, ['image/gif', 'image/png', 'image/jpeg', 'image/webp'], val); + return Nette.validators.mimeType(elem, arg || ['image/gif', 'image/png', 'image/jpeg', 'image/webp'], val); }, 'static': function (elem, arg) { diff --git a/tests/Forms/Controls.UploadControl.render.phpt b/tests/Forms/Controls.UploadControl.render.phpt index ffcb27951..b43115c70 100644 --- a/tests/Forms/Controls.UploadControl.render.phpt +++ b/tests/Forms/Controls.UploadControl.render.phpt @@ -71,7 +71,7 @@ test('accepted files', function () { Assert::match('', (string) $input->getControl()); $input = $form->addUpload('file3')->addRule(Form::Image); - Assert::match('', (string) $input->getControl()); + Assert::match('', (string) $input->getControl()); }); diff --git a/tests/Forms/Forms.renderer.1.expect b/tests/Forms/Forms.renderer.1.expect index 98d298a58..8b0082a2e 100644 --- a/tests/Forms/Forms.renderer.1.expect +++ b/tests/Forms/Forms.renderer.1.expect @@ -81,7 +81,7 @@