Create functions to get the form field validators
authorCyperghost <olaf_schmitz_1@t-online.de>
Thu, 14 Nov 2024 09:20:34 +0000 (10:20 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Thu, 14 Nov 2024 09:20:34 +0000 (10:20 +0100)
wcfsetup/install/files/lib/acp/action/TemplateGroupCopyAction.class.php
wcfsetup/install/files/lib/acp/form/TemplateGroupAddForm.class.php

index 72101216b01c6b2b531c32ee5ed735b31645694c..7db0467fd2eaa9d1947ac8e41bb14c3466f194ee 100644 (file)
@@ -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();
index 6ae29ee63b47345faf9dca7c99cb0cd310a42ea8..8ad7be21809601c30d4e045a3fb7b92363023478 100644 (file)
@@ -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'
+                        )
+                    );
+                }
+            }
+        );
     }
 }