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 Nov 21, 2024
1 parent 96106f3 commit b68c9fd
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 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 @@ -320,28 +321,25 @@ 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);
$result = ValidationResult::create();
$this->beforeExtending('updateValidate', function () use ($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 $result->combineAnd(parent::validate());
}

/**
Expand Down

0 comments on commit b68c9fd

Please sign in to comment.