Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Use updated FormField::validate() signature #1508

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading