From 9d3760b1543e6858f8ed3adae63c76a4fd542460 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 21 Nov 2024 18:14:53 +1300 Subject: [PATCH] API Use updated FormField::validate() signature --- code/Forms/UploadField.php | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/code/Forms/UploadField.php b/code/Forms/UploadField.php index 2f235b368..6b69886d3 100644 --- a/code/Forms/UploadField.php +++ b/code/Forms/UploadField.php @@ -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; @@ -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(); } /**