Add validation of submitted files
authorCyperghost <olaf_schmitz_1@t-online.de>
Wed, 19 Jun 2024 10:38:47 +0000 (12:38 +0200)
committerCyperghost <olaf_schmitz_1@t-online.de>
Wed, 19 Jun 2024 10:38:47 +0000 (12:38 +0200)
wcfsetup/install/files/lib/system/form/builder/field/FileProcessorFormField.class.php

index dfd9d1531f9a3e7c364d709aa46e19c6fc4ee709..ca4f9428b167d0cf29d440f659a17fce72549c59 100644 (file)
@@ -7,6 +7,7 @@ use wcf\data\file\FileList;
 use wcf\data\file\thumbnail\FileThumbnailList;
 use wcf\system\file\processor\FileProcessor;
 use wcf\system\file\processor\IFileProcessor;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
 use wcf\system\form\builder\TObjectTypeFormNode;
 use wcf\util\ArrayUtil;
 use wcf\util\ImageUtil;
@@ -115,6 +116,8 @@ final class FileProcessorFormField extends AbstractFormField
             if ($file->fileID === $value) {
                 $this->files = [$file];
                 $fileIDs[] = $value;
+            } else {
+                $value = null;
             }
         } else {
             if (!\is_array($value)) {
@@ -126,7 +129,8 @@ final class FileProcessorFormField extends AbstractFormField
             $fileList->readObjects();
             $this->files = $fileList->getObjects();
 
-            $fileIDs = $fileList->getObjectIDs();
+            // remove obsolete file IDs from $value
+            $fileIDs = $value = $fileList->getObjectIDs();
         }
 
         if ($fileIDs !== []) {
@@ -141,6 +145,32 @@ final class FileProcessorFormField extends AbstractFormField
         return parent::value($value);
     }
 
+    #[\Override]
+    public function validate()
+    {
+        if ($this->isRequired() && $this->files === []) {
+            $this->addValidationError(new FormFieldValidationError('empty'));
+        }
+
+        $fileProcessor = $this->getFileProcessor();
+
+        if (\count($this->files) > $fileProcessor->getMaximumCount($this->context)) {
+            $this->addValidationError(
+                new FormFieldValidationError(
+                    'maximumFiles',
+                    'wcf.form.field.fileProcessor.error.maximumFiles',
+                    [
+                        'maximumCount' => $fileProcessor->getMaximumCount($this->context),
+                        'count' => \count($this->files),
+                    ]
+                )
+            );
+        }
+
+        parent::validate();
+    }
+
+
     /**
      * Returns the context for the file processor.
      */