From f5db9b3472c8254f5c1e43869e870813e9a9fabd Mon Sep 17 00:00:00 2001 From: David Mellen Date: Wed, 13 Nov 2024 13:02:18 +0100 Subject: [PATCH] feat: add MimeType validator to FileUpload form element --- Classes/Form/FormDefinitionDecorator.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Classes/Form/FormDefinitionDecorator.php b/Classes/Form/FormDefinitionDecorator.php index 26eadf6..8cd747b 100644 --- a/Classes/Form/FormDefinitionDecorator.php +++ b/Classes/Form/FormDefinitionDecorator.php @@ -41,6 +41,7 @@ protected function overrideDefinition(array $decorated, array $definition, int $ $this->setNotEmptyValidationErrorMessages($element); $this->setCheckboxLinks($element); $this->setFileSizeValidatorBytes($element); + $this->setMimeTypeValidator($element); } return $decorated; } @@ -104,4 +105,23 @@ private function setFileSizeValidatorBytes(array &$element): void } } } + + /** + * @param mixed[] $element + */ + private function setMimeTypeValidator(array &$element): void + { + if ($element['type'] === 'FileUpload') { + $mimeTypes = $element['properties']['allowedMimeTypes'] ?? []; + + if (!empty($mimeTypes)) { + $element['validators'][] = [ + 'identifier' => 'MimeType', + 'options' => [ + 'allowed' => $mimeTypes, + ], + ]; + } + } + } }