Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Mabahe/ebb34cf346f62a19774be86373a7c232 to your computer and use it in GitHub Desktop.
Save Mabahe/ebb34cf346f62a19774be86373a7c232 to your computer and use it in GitHub Desktop.
TYPO3 8.7.20 Backport for typo3/cms-form: [FEATURE] EXT:form - add file size validator
Index: Classes/Mvc/ProcessingRule.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Classes/Mvc/ProcessingRule.php (date 1540895975000)
+++ Classes/Mvc/ProcessingRule.php (date 1543499098000)
@@ -143,6 +143,18 @@
$this->validator->addValidator($validator);
}
+ /**
+ * Removes the specified validator.
+ *
+ * @param ValidatorInterface $validator The validator to remove
+ * @throws \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException
+ * @internal
+ */
+ public function removeValidator(ValidatorInterface $validator)
+ {
+ $this->validator->removeValidator($validator);
+ }
+
/**
* @param mixed $value
* @return mixed
Index: Configuration/Yaml/FormEditorSetup.yaml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Configuration/Yaml/FormEditorSetup.yaml (date 1540895975000)
+++ Configuration/Yaml/FormEditorSetup.yaml (date 1543499098000)
@@ -109,6 +109,8 @@
errorMessage: 'formEditor.formElementPropertyValidatorsDefinition.FormElementIdentifierWithinCurlyBraces.label'
FormElementIdentifierWithinCurlyBracesExclusive:
errorMessage: 'formEditor.formElementPropertyValidatorsDefinition.FormElementIdentifierWithinCurlyBraces.label'
+ FileSize:
+ errorMessage: 'formEditor.formElementPropertyValidatorsDefinition.FileSize.label'
formElementGroups:
input:
@@ -832,6 +834,14 @@
options:
minimum: ''
maximum: ''
+ FileSize:
+ formEditor:
+ iconIdentifier: 't3-form-icon-validator'
+ label: 'formEditor.elements.FileUploadMixin.validators.FileSize.editor.header.label'
+ predefinedDefaults:
+ options:
+ minimum: '0B'
+ maximum: '10M'
########### MIXINS ###########
mixins:
@@ -1134,6 +1144,34 @@
10:
value: '1:/user_upload/'
label: '1:/user_upload/'
+ 900:
+ identifier: 'validators'
+ templateName: 'Inspector-ValidatorsEditor'
+ label: 'formEditor.elements.FileUploadMixin.editor.validators.label'
+ selectOptions:
+ 10:
+ value: ''
+ label: 'formEditor.elements.FileUploadMixin.editor.validators.EmptyValue.label'
+ 20:
+ value: 'FileSize'
+ label: 'formEditor.elements.FileUploadMixin.editor.validators.FileSize.label'
+
+ propertyCollections:
+ validators:
+ 10:
+ identifier: 'FileSize'
+ editors:
+ __inheritances:
+ 10: 'TYPO3.CMS.Form.mixins.formElementMixins.BaseCollectionEditorsMixin'
+ 20: 'TYPO3.CMS.Form.mixins.formElementMixins.MinimumMaximumEditorsMixin'
+ 100:
+ label: 'formEditor.elements.FileUploadMixin.validators.FileSize.editor.header.label'
+ 200:
+ propertyValidators:
+ 10: 'FileSize'
+ 300:
+ propertyValidators:
+ 10: 'FileSize'
formEmailFinisherMixin:
editors:
Index: Configuration/Yaml/BaseSetup.yaml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Configuration/Yaml/BaseSetup.yaml (date 1540895975000)
+++ Configuration/Yaml/BaseSetup.yaml (date 1543499098000)
@@ -327,6 +327,11 @@
#options:
#minimum: 0
#maximum: 0
+ FileSize:
+ implementationClassName: 'TYPO3\CMS\Form\Mvc\Validation\FileSizeValidator'
+ #options:
+ #minimum: '0B'
+ #maximum: '10M'
########### MIXINS ###########
mixins:
Index: Classes/Mvc/Property/TypeConverter/UploadedFileReferenceConverter.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Classes/Mvc/Property/TypeConverter/UploadedFileReferenceConverter.php (date 1540895975000)
+++ Classes/Mvc/Property/TypeConverter/UploadedFileReferenceConverter.php (date 1543499098000)
@@ -15,6 +15,7 @@
* The TYPO3 project - inspiring people to share!
*/
+use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Resource\File as File;
use TYPO3\CMS\Core\Resource\FileReference as CoreFileReference;
use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -25,6 +26,7 @@
use TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface;
use TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter;
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
+use TYPO3\CMS\Form\Service\TranslationService;
/**
* Class UploadedFileReferenceConverter
@@ -274,23 +276,32 @@
*/
protected function getUploadErrorMessage(int $errorCode): string
{
+ $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(static::class);
switch ($errorCode) {
case \UPLOAD_ERR_INI_SIZE:
- return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
+ $logger->error('The uploaded file exceeds the upload_max_filesize directive in php.ini.', []);
+ return TranslationService::getInstance()->translate('upload.error.150530345', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
case \UPLOAD_ERR_FORM_SIZE:
- return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
+ $logger->error('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', []);
+ return TranslationService::getInstance()->translate('upload.error.150530345', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
case \UPLOAD_ERR_PARTIAL:
- return 'The uploaded file was only partially uploaded';
+ $logger->error('The uploaded file was only partially uploaded.', []);
+ return TranslationService::getInstance()->translate('upload.error.150530346', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
case \UPLOAD_ERR_NO_FILE:
- return 'No file was uploaded';
+ $logger->error('No file was uploaded.', []);
+ return TranslationService::getInstance()->translate('upload.error.150530347', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
case \UPLOAD_ERR_NO_TMP_DIR:
- return 'Missing a temporary folder';
+ $logger->error('Missing a temporary folder.', []);
+ return TranslationService::getInstance()->translate('upload.error.150530348', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
case \UPLOAD_ERR_CANT_WRITE:
- return 'Failed to write file to disk';
+ $logger->error('Failed to write file to disk.', []);
+ return TranslationService::getInstance()->translate('upload.error.150530348', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
case \UPLOAD_ERR_EXTENSION:
- return 'File upload stopped by extension';
+ $logger->error('File upload stopped by extension.', []);
+ return TranslationService::getInstance()->translate('upload.error.150530348', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
default:
- return 'Unknown upload error';
+ $logger->error('Unknown upload error.', []);
+ return TranslationService::getInstance()->translate('upload.error.150530348', null, 'EXT:form/Resources/Private/Language/locallang.xlf');
}
}
}
Index: Resources/Private/Language/locallang.xlf
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Resources/Private/Language/locallang.xlf (date 1540895975000)
+++ Resources/Private/Language/locallang.xlf (date 1543499098000)
@@ -14,58 +14,58 @@
</trans-unit>
<trans-unit id="validation.error.1221560910" xml:space="preserve">
- <source>This field is mandatory</source>
+ <source>This field is mandatory.</source>
</trans-unit>
<trans-unit id="validation.error.1221560718" xml:space="preserve">
- <source>This field is mandatory</source>
+ <source>This field is mandatory.</source>
</trans-unit>
<trans-unit id="validation.error.1347992400" xml:space="preserve">
- <source>This field is mandatory</source>
+ <source>This field is mandatory.</source>
</trans-unit>
<trans-unit id="validation.error.1347992453" xml:space="preserve">
- <source>This field is mandatory</source>
+ <source>This field is mandatory.</source>
</trans-unit>
<trans-unit id="validation.error.1238087674" xml:space="preserve">
- <source>Please enter a valid Date</source>
+ <source>Please enter a valid Date.</source>
</trans-unit>
<trans-unit id="validation.error.1221551320" xml:space="preserve">
- <source>Please enter letters or digits</source>
+ <source>Please enter letters or digits.</source>
</trans-unit>
<trans-unit id="validation.error.1221565786" xml:space="preserve">
- <source>Please enter a valid text (e.g. without XML tags)</source>
+ <source>Please enter a valid text (e.g. without XML tags).</source>
</trans-unit>
<trans-unit id="validation.error.1238110957" xml:space="preserve">
- <source>Please enter a valid text</source>
+ <source>Please enter a valid text.</source>
</trans-unit>
<trans-unit id="validation.error.1269883975" xml:space="preserve">
- <source>Please enter a valid text</source>
+ <source>Please enter a valid text.</source>
</trans-unit>
<trans-unit id="validation.error.1428504122" xml:space="preserve">
- <source>Please enter a text between %s and %s characters</source>
+ <source>Please enter a text between %s and %s characters.</source>
</trans-unit>
<trans-unit id="validation.error.1238108068" xml:space="preserve">
- <source>Please enter a text which is longer than %s characters</source>
+ <source>Please enter a text which is longer than %s characters.</source>
</trans-unit>
<trans-unit id="validation.error.1238108069" xml:space="preserve">
- <source>Please enter a text which is not longer than %s characters</source>
+ <source>Please enter a text which is not longer than %s characters.</source>
</trans-unit>
<trans-unit id="validation.error.1221559976" xml:space="preserve">
- <source>Please enter a valid email address</source>
+ <source>Please enter a valid email address.</source>
</trans-unit>
<trans-unit id="validation.error.1221560494" xml:space="preserve">
- <source>Please enter a valid number</source>
+ <source>Please enter a valid number.</source>
</trans-unit>
<trans-unit id="validation.error.1221560288" xml:space="preserve">
- <source>Please enter a valid floating point number</source>
+ <source>Please enter a valid floating point number.</source>
</trans-unit>
<trans-unit id="validation.error.1221563685" xml:space="preserve">
- <source>Please enter a valid number</source>
+ <source>Please enter a valid number.</source>
</trans-unit>
<trans-unit id="validation.error.1221561046" xml:space="preserve">
- <source>Please enter a valid number between %s and %s</source>
+ <source>Please enter a valid number between %s and %s.</source>
</trans-unit>
<trans-unit id="validation.error.1221565130" xml:space="preserve">
- <source>Please enter a valid value</source>
+ <source>Please enter a valid value.</source>
</trans-unit>
<trans-unit id="validation.error.1475002976" xml:space="preserve">
<source>The given subject is not countable.</source>
@@ -80,13 +80,34 @@
<source>The media type "%s" is not allowed for this file.</source>
</trans-unit>
<trans-unit id="validation.error.1476396435" xml:space="preserve">
- <source>Do not fill this field</source>
+ <source>Do not fill this field.</source>
+ </trans-unit>
+ <trans-unit id="validation.error.1505303626" xml:space="preserve">
+ <source>The given value was not an instance of \TYPO3\CMS\Extbase\Domain\Model\FileReference or \TYPO3\CMS\Core\Resource\File.</source>
+ </trans-unit>
+ <trans-unit id="validation.error.1505305752" xml:space="preserve">
+ <source>The file must be at least %s in size.</source>
+ </trans-unit>
+ <trans-unit id="validation.error.1505305753" xml:space="preserve">
+ <source>The file size can not exceed %s in size.</source>
</trans-unit>
<trans-unit id="form_new_wizard_title" xml:space="preserve">
<source>Form</source>
</trans-unit>
<trans-unit id="form_new_wizard_description" xml:space="preserve">
<source>A form allowing website users to submit messages.</source>
+ </trans-unit>
+ <trans-unit id="upload.error.150530345" xml:space="preserve">
+ <source>Maximum file size exceeded.</source>
+ </trans-unit>
+ <trans-unit id="upload.error.150530346" xml:space="preserve">
+ <source>The file is partially uploaded, please try again.</source>
+ </trans-unit>
+ <trans-unit id="upload.error.150530347" xml:space="preserve">
+ <source>No file was uploaded.</source>
+ </trans-unit>
+ <trans-unit id="upload.error.150530348" xml:space="preserve">
+ <source>Upload not possible.</source>
</trans-unit>
</body>
</file>
Index: Resources/Private/Language/Database.xlf
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Resources/Private/Language/Database.xlf (date 1540895975000)
+++ Resources/Private/Language/Database.xlf (date 1543499098000)
@@ -246,6 +246,9 @@
<trans-unit id="formEditor.formElementPropertyValidatorsDefinition.FormElementIdentifierWithinCurlyBraces.label" xml:space="preserve">
<source>Invalid form element</source>
</trans-unit>
+ <trans-unit id="formEditor.formElementPropertyValidatorsDefinition.FileSize.label" xml:space="preserve">
+ <source>Invalid file size format, valid e.g. "10B|K|M|G"</source>
+ </trans-unit>
<trans-unit id="formEditor.formElementGroups.input.label" xml:space="preserve">
<source>Basic elements</source>
@@ -440,6 +443,18 @@
<trans-unit id="formEditor.elements.FileUploadMixin.editor.saveToFileMount.label" xml:space="preserve">
<source>Uploads save path</source>
</trans-unit>
+ <trans-unit id="formEditor.elements.FileUploadMixin.editor.validators.label" xml:space="preserve">
+ <source>Validators</source>
+ </trans-unit>
+ <trans-unit id="formEditor.elements.FileUploadMixin.editor.validators.EmptyValue.label" xml:space="preserve">
+ <source>Add validator</source>
+ </trans-unit>
+ <trans-unit id="formEditor.elements.FileUploadMixin.editor.validators.FileSize.label" xml:space="preserve">
+ <source>File size</source>
+ </trans-unit>
+ <trans-unit id="formEditor.elements.FileUploadMixin.validators.FileSize.editor.header.label" xml:space="preserve">
+ <source>File size</source>
+ </trans-unit>
<trans-unit id="formEditor.elements.Form.editor.submitButtonLabel.label" xml:space="preserve">
<source>Submit label</source>
Index: Classes/Mvc/Property/PropertyMappingConfiguration.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Classes/Mvc/Property/PropertyMappingConfiguration.php (date 1540895975000)
+++ Classes/Mvc/Property/PropertyMappingConfiguration.php (date 1543499603000)
@@ -19,6 +19,7 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
+use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
use TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload;
use TYPO3\CMS\Form\Domain\Model\Renderable\RenderableInterface;
use TYPO3\CMS\Form\Mvc\Property\TypeConverter\UploadedFileReferenceConverter;
@@ -66,6 +67,14 @@
$validators = [$mimeTypeValidator];
}
+ $processingRule = $renderable->getRootForm()->getProcessingRule($renderable->getIdentifier());
+ foreach ($processingRule->getValidators() as $validator) {
+ if (!($validator instanceof NotEmptyValidator)) {
+ $validators[] = $validator;
+ $processingRule->removeValidator($validator);
+ }
+ }
+
$uploadConfiguration = [
UploadedFileReferenceConverter::CONFIGURATION_FILE_VALIDATORS => $validators,
UploadedFileReferenceConverter::CONFIGURATION_UPLOAD_CONFLICT_MODE => 'rename',
Index: Resources/Public/JavaScript/Backend/FormEditor/ViewModel.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Resources/Public/JavaScript/Backend/FormEditor/ViewModel.js (date 1540895975000)
+++ Resources/Public/JavaScript/Backend/FormEditor/ViewModel.js (date 1543499098000)
@@ -257,6 +257,12 @@
return getFormEditorApp().getFormElementPropertyValidatorDefinition('FormElementIdentifierWithinCurlyBracesInclusive')['errorMessage'] || 'invalid value';
}
});
+
+ getFormEditorApp().addPropertyValidationValidator('FileSize', function(formElement, propertyPath) {
+ if (!formElement.get(propertyPath).match(/^(\d*\.?\d+)(B|K|M|G)$/i)) {
+ return getFormEditorApp().getFormElementPropertyValidatorDefinition('FileSize')['errorMessage'] || 'invalid value';
+ }
+ });
};
/**
Index: Tests/Unit/Mvc/Validation/FileSizeValidatorTest.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Tests/Unit/Mvc/Validation/FileSizeValidatorTest.php (date 1543499098000)
+++ Tests/Unit/Mvc/Validation/FileSizeValidatorTest.php (date 1543499098000)
@@ -0,0 +1,127 @@
+<?php
+namespace TYPO3\CMS\Form\Tests\Unit\Mvc\Validation;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Resource\File;
+use TYPO3\CMS\Core\Resource\ResourceStorage;
+use TYPO3\CMS\Form\Mvc\Validation\Exception\InvalidValidationOptionsException;
+use TYPO3\CMS\Form\Mvc\Validation\FileSizeValidator;
+
+/**
+ * Test case
+ */
+class FileSizeValidatorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+{
+
+ /**
+ * @test
+ */
+ public function FileSizeValidatorThrowsExceptionIfMinimumOptionIsInvalid()
+ {
+ $this->expectException(InvalidValidationOptionsException::class);
+ $this->expectExceptionCode(1505304205);
+
+ $options = ['minimum' => '0', 'maximum' => '1B'];
+ $validator = $this->getMockBuilder(FileSizeValidator::class)
+ ->setMethods(['translateErrorMessage'])
+ ->setConstructorArgs(['options' => $options])
+ ->getMock();
+
+ $validator->validate(true);
+ }
+
+ /**
+ * @test
+ */
+ public function FileSizeValidatorThrowsExceptionIfMaximumOptionIsInvalid()
+ {
+ $this->expectException(InvalidValidationOptionsException::class);
+ $this->expectExceptionCode(1505304206);
+
+ $options = ['minimum' => '0B', 'maximum' => '1'];
+ $validator = $this->getMockBuilder(FileSizeValidator::class)
+ ->setMethods(['translateErrorMessage'])
+ ->setConstructorArgs(['options' => $options])
+ ->getMock();
+
+ $validator->validate(true);
+ }
+
+ /**
+ * @test
+ */
+ public function FileSizeValidatorHasErrosIfFileResourceSizeIsToSmall()
+ {
+ $options = ['minimum' => '1M', 'maximum' => '10M'];
+ $validator = $this->getMockBuilder(FileSizeValidator::class)
+ ->setMethods(['translateErrorMessage'])
+ ->setConstructorArgs(['options' => $options])
+ ->getMock();
+
+ $mockedStorage = $this->getMockBuilder(ResourceStorage::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $file = new File(['identifier' => '/foo', 'size' => '1'], $mockedStorage);
+ $this->assertTrue($validator->validate($file)->hasErrors());
+ }
+
+ /**
+ * @test
+ */
+ public function FileSizeValidatorHasErrosIfFileResourceSizeIsToBig()
+ {
+ $options = ['minimum' => '1M', 'maximum' => '1M'];
+ $validator = $this->getMockBuilder(FileSizeValidator::class)
+ ->setMethods(['translateErrorMessage'])
+ ->setConstructorArgs(['options' => $options])
+ ->getMock();
+
+ $mockedStorage = $this->getMockBuilder(ResourceStorage::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $file = new File(['identifier' => '/foo', 'size' => '1048577'], $mockedStorage);
+ $this->assertTrue($validator->validate($file)->hasErrors());
+ }
+
+ /**
+ * @test
+ */
+ public function FileSizeValidatorHasNoErrorsIfInputIsEmptyString()
+ {
+ $options = ['minimum' => '0B', 'maximum' => '1M'];
+ $validator = $this->getMockBuilder(FileSizeValidator::class)
+ ->setMethods(['translateErrorMessage'])
+ ->setConstructorArgs(['options' => $options])
+ ->getMock();
+
+ $this->assertFalse($validator->validate('')->hasErrors());
+ }
+
+ /**
+ * @test
+ */
+ public function FileSizeValidatorHasErrorsIfInputIsNoFileResource()
+ {
+ $options = ['minimum' => '0B', 'maximum' => '1M'];
+ $validator = $this->getMockBuilder(FileSizeValidator::class)
+ ->setMethods(['translateErrorMessage'])
+ ->setConstructorArgs(['options' => $options])
+ ->getMock();
+
+ $this->assertTrue($validator->validate('string')->hasErrors());
+ }
+}
Index: Classes/Mvc/Validation/FileSizeValidator.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- Classes/Mvc/Validation/FileSizeValidator.php (date 1543499770000)
+++ Classes/Mvc/Validation/FileSizeValidator.php (date 1543499770000)
@@ -0,0 +1,105 @@
+<?php
+declare(strict_types = 1);
+namespace TYPO3\CMS\Form\Mvc\Validation;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Resource\File;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Extbase\Domain\Model\FileReference;
+use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
+use TYPO3\CMS\Form\Mvc\Validation\Exception\InvalidValidationOptionsException;
+
+/**
+ * Validator for countable types
+ *
+ * Scope: frontend
+ * @internal
+ */
+class FileSizeValidator extends AbstractValidator
+{
+ /**
+ * @var array
+ */
+ protected $supportedOptions = [
+ 'minimum' => ['0B', 'The minimum file size to accept', 'string'],
+ 'maximum' => [PHP_INT_MAX . 'B', 'The maximum file size to accept', 'string']
+ ];
+
+ /**
+ * The given value is valid
+ *
+ * @param FileReference|File $resource
+ * @api
+ */
+ public function isValid($resource)
+ {
+ $this->validateOptions();
+ if ($resource instanceof FileReference) {
+ $resource = $resource->getOriginalResource();
+ } elseif (!$resource instanceof File) {
+ $this->addError(
+ $this->translateErrorMessage(
+ 'validation.error.1505303626',
+ 'form'
+ ),
+ 1505303626
+ );
+ return;
+ }
+
+ $fileSize = $resource->getSize();
+ $minFileSize = GeneralUtility::getBytesFromSizeMeasurement($this->options['minimum']);
+ $maxFileSize = GeneralUtility::getBytesFromSizeMeasurement($this->options['maximum']);
+
+ $labels = ' Bytes| Kilobyte| Megabyte| Gigabyte';
+ if ($fileSize < $minFileSize) {
+ $this->addError(
+ $this->translateErrorMessage(
+ 'validation.error.1505305752',
+ 'form',
+ [GeneralUtility::formatSize($minFileSize, $labels)]
+ ),
+ 1505305752,
+ [GeneralUtility::formatSize($minFileSize, $labels)]
+ );
+ }
+ if ($fileSize > $maxFileSize) {
+ $this->addError(
+ $this->translateErrorMessage(
+ 'validation.error.1505305753',
+ 'form',
+ [GeneralUtility::formatSize($maxFileSize, $labels)]
+ ),
+ 1505305753,
+ [GeneralUtility::formatSize($maxFileSize, $labels)]
+ );
+ }
+ }
+
+ /**
+ * Checks if this validator is correctly configured
+ *
+ * @throws InvalidValidationOptionsException if the configured validation options are incorrect
+ */
+ protected function validateOptions()
+ {
+ if (!preg_match('/^(\d*\.?\d+)(B|K|M|G)$/i', $this->options['minimum'])) {
+ throw new InvalidValidationOptionsException('The option "minimum" has an invalid format. Valid formats are something like this: "10B|K|M|G".', 1505304205);
+ }
+ if (!preg_match('/^(\d*\.?\d+)(B|K|M|G)$/i', $this->options['maximum'])) {
+ throw new InvalidValidationOptionsException('The option "maximum" has an invalid format. Valid formats are something like this: "10B|K|M|G".', 1505304206);
+ }
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment