From: Cyperghost Date: Thu, 14 Nov 2024 09:20:34 +0000 (+0100) Subject: Create functions to get the form field validators X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=9a47050a5aa4dc10d1a3e005d28366cc5ca36554;p=GitHub%2FWoltLab%2FWCF.git Create functions to get the form field validators --- diff --git a/wcfsetup/install/files/lib/acp/action/TemplateGroupCopyAction.class.php b/wcfsetup/install/files/lib/acp/action/TemplateGroupCopyAction.class.php index 72101216b0..7db0467fd2 100644 --- a/wcfsetup/install/files/lib/acp/action/TemplateGroupCopyAction.class.php +++ b/wcfsetup/install/files/lib/acp/action/TemplateGroupCopyAction.class.php @@ -7,6 +7,7 @@ use Laminas\Diactoros\Response\JsonResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; +use wcf\acp\form\TemplateGroupAddForm; use wcf\data\template\group\TemplateGroup; use wcf\data\template\group\TemplateGroupAction; use wcf\data\template\TemplateAction; @@ -15,12 +16,9 @@ use wcf\http\Helper; use wcf\system\exception\IllegalLinkException; use wcf\system\exception\PermissionDeniedException; use wcf\system\form\builder\field\TextFormField; -use wcf\system\form\builder\field\validation\FormFieldValidationError; -use wcf\system\form\builder\field\validation\FormFieldValidator; use wcf\system\form\builder\Psr15DialogForm; use wcf\system\request\LinkHandler; use wcf\system\WCF; -use wcf\util\FileUtil; /** * Handles the copying of template groups. @@ -120,62 +118,13 @@ final class TemplateGroupCopyAction implements RequestHandlerInterface ->label('wcf.global.name') ->required() ->value($templateGroup->templateGroupName) - ->addValidator( - new FormFieldValidator('templateNameValidator', function (TextFormField $formField) { - $sql = "SELECT COUNT(*) - FROM wcf1_template_group - WHERE templateGroupName = ?"; - $statement = WCF::getDB()->prepare($sql); - $statement->execute([$formField->getValue()]); - - if ($statement->fetchSingleColumn()) { - $formField->addValidationError( - new FormFieldValidationError( - 'notUnique', - 'wcf.acp.template.group.name.error.notUnique' - ) - ); - } - }) - ), + ->addValidator(TemplateGroupAddForm::getTemplateNameValidator()), TextFormField::create('templateGroupFolderName') ->label('wcf.acp.template.group.folderName') ->required() ->value($templateGroup->templateGroupFolderName) - ->addValidator( - new FormFieldValidator('folderNameValidator', function (TextFormField $formField) { - $formField->value(FileUtil::addTrailingSlash($formField->getValue())); - - if (!\preg_match('/^[a-z0-9_\- ]+\/$/i', $formField->getValue())) { - $formField->addValidationError( - new FormFieldValidationError( - 'invalid', - 'wcf.acp.template.group.folderName.error.invalid' - ) - ); - } - }) - ) - ->addValidator( - new FormFieldValidator('uniqueFolderNameValidator', function (TextFormField $formField) { - $formField->value(FileUtil::addTrailingSlash($formField->getValue())); - - $sql = "SELECT COUNT(*) - FROM wcf1_template_group - WHERE templateGroupFolderName = ?"; - $statement = WCF::getDB()->prepare($sql); - $statement->execute([$formField->getValue()]); - - if ($statement->fetchSingleColumn()) { - $formField->addValidationError( - new FormFieldValidationError( - 'notUnique', - 'wcf.acp.template.group.folderName.error.notUnique' - ) - ); - } - }) - ), + ->addValidator(TemplateGroupAddForm::getFolderNameValidator()) + ->addValidator(TemplateGroupAddForm::getUniqueFolderNameValidator()), ]); $form->build(); diff --git a/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php b/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php index 6ae29ee63b..8ad7be2180 100644 --- a/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php @@ -58,69 +58,84 @@ class TemplateGroupAddForm extends AbstractFormBuilderForm TextFormField::create('templateGroupName') ->label('wcf.global.name') ->required() - ->addValidator( - new FormFieldValidator('templateNameValidator', function (TextFormField $formField) { - if ($formField->getValue() === $this->formObject?->templateGroupName) { - return; - } - - $sql = "SELECT COUNT(*) - FROM wcf1_template_group - WHERE templateGroupName = ?"; - $statement = WCF::getDB()->prepare($sql); - $statement->execute([$formField->getValue()]); - - if ($statement->fetchSingleColumn()) { - $formField->addValidationError( - new FormFieldValidationError( - 'notUnique', - 'wcf.acp.template.group.name.error.notUnique' - ) - ); - } - }) - ), + ->addValidator(TemplateGroupAddForm::getTemplateNameValidator($this->formObject)), TextFormField::create('templateGroupFolderName') ->label('wcf.acp.template.group.folderName') ->required() - ->addValidator( - new FormFieldValidator('folderNameValidator', function (TextFormField $formField) { - $formField->value(FileUtil::addTrailingSlash($formField->getValue())); - - if (!\preg_match('/^[a-z0-9_\- ]+\/$/i', $formField->getValue())) { - $formField->addValidationError( - new FormFieldValidationError( - 'invalid', - 'wcf.acp.template.group.folderName.error.invalid' - ) - ); - } - }) - ) - ->addValidator( - new FormFieldValidator('uniqueFolderNameValidator', function (TextFormField $formField) { - $formField->value(FileUtil::addTrailingSlash($formField->getValue())); - - if ($formField->getValue() === $this->formObject?->templateGroupFolderName) { - return; - } - - $sql = "SELECT COUNT(*) + ->addValidator(TemplateGroupAddForm::getFolderNameValidator()) + ->addValidator(TemplateGroupAddForm::getUniqueFolderNameValidator($this->formObject)), + ]); + } + + public static function getFolderNameValidator(): FormFieldValidator + { + return new FormFieldValidator('folderNameValidator', function (TextFormField $formField) { + $formField->value(FileUtil::addTrailingSlash($formField->getValue())); + + if (!\preg_match('/^[a-z0-9_\- ]+\/$/i', $formField->getValue())) { + $formField->addValidationError( + new FormFieldValidationError( + 'invalid', + 'wcf.acp.template.group.folderName.error.invalid' + ) + ); + } + }); + } + + public static function getUniqueFolderNameValidator(?TemplateGroup $formObject = null): FormFieldValidator + { + return new FormFieldValidator( + 'uniqueFolderNameValidator', + function (TextFormField $formField) use ($formObject) { + $formField->value(FileUtil::addTrailingSlash($formField->getValue())); + + if ($formField->getValue() === $formObject?->templateGroupFolderName) { + return; + } + + $sql = "SELECT COUNT(*) FROM wcf1_template_group WHERE templateGroupFolderName = ?"; - $statement = WCF::getDB()->prepare($sql); - $statement->execute([$formField->getValue()]); - - if ($statement->fetchSingleColumn()) { - $formField->addValidationError( - new FormFieldValidationError( - 'notUnique', - 'wcf.acp.template.group.folderName.error.notUnique' - ) - ); - } - }) - ), - ]); + $statement = WCF::getDB()->prepare($sql); + $statement->execute([$formField->getValue()]); + + if ($statement->fetchSingleColumn()) { + $formField->addValidationError( + new FormFieldValidationError( + 'notUnique', + 'wcf.acp.template.group.folderName.error.notUnique' + ) + ); + } + } + ); + } + + public static function getTemplateNameValidator(?TemplateGroup $formObject = null): FormFieldValidator + { + return new FormFieldValidator( + 'templateNameValidator', + function (TextFormField $formField) use ($formObject) { + if ($formField->getValue() === $formObject?->templateGroupName) { + return; + } + + $sql = "SELECT COUNT(*) + FROM wcf1_template_group + WHERE templateGroupName = ?"; + $statement = WCF::getDB()->prepare($sql); + $statement->execute([$formField->getValue()]); + + if ($statement->fetchSingleColumn()) { + $formField->addValidationError( + new FormFieldValidationError( + 'notUnique', + 'wcf.acp.template.group.name.error.notUnique' + ) + ); + } + } + ); } }