Skip to content

Commit

Permalink
API Use updated FormField::validate() signature
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 1, 2024
1 parent 7ada4e1 commit 9d3760b
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions code/Forms/UploadField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\Assets\Folder;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Forms\FileHandleField;
use SilverStripe\Forms\FileUploadReceiver;
use SilverStripe\Forms\FormField;
Expand Down Expand Up @@ -318,28 +319,24 @@ public function performDisabledTransformation()

/**
* Checks if the number of files attached adheres to the $allowedMaxFileNumber defined
*
* @param Validator $validator
* @return bool
*/
public function validate($validator)
public function validate(): ValidationResult
{
$maxFiles = $this->getAllowedMaxFileNumber();
$count = count($this->getItems() ?? []);

if ($maxFiles < 1 || $count <= $maxFiles) {
return $this->extendValidationResult(true, $validator);
}

$validator->validationError($this->getName(), _t(
__CLASS__ . '.ErrorMaxFilesReached',
'You can only upload {count} file.|You can only upload {count} files.',
[
'count' => $maxFiles,
]
));

return $this->extendValidationResult(false, $validator);
$this->beforeExtending('updateValidate', function (ValidationResult $result) {
$maxFiles = $this->getAllowedMaxFileNumber();
$count = count($this->getItems() ?? []);
if ($maxFiles < 1 || $count <= $maxFiles) {
return;
}
$result->addFieldError($this->getName(), _t(
__CLASS__ . '.ErrorMaxFilesReached',
'You can only upload {count} file.|You can only upload {count} files.',
[
'count' => $maxFiles,
]
));
});
return parent::validate();
}

/**
Expand Down

0 comments on commit 9d3760b

Please sign in to comment.