Fix issues with captcha form field (validation)
authorMatthias Schmidt <gravatronics@live.com>
Tue, 2 Apr 2019 17:29:42 +0000 (19:29 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Tue, 2 Apr 2019 17:29:42 +0000 (19:29 +0200)
See #2509

com.woltlab.wcf/templates/__captchaFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/acp/templates/__captchaFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/CaptchaFormField.class.php

diff --git a/com.woltlab.wcf/templates/__captchaFormField.tpl b/com.woltlab.wcf/templates/__captchaFormField.tpl
new file mode 100644 (file)
index 0000000..f4202f8
--- /dev/null
@@ -0,0 +1 @@
+{@$field->getObjectType()->getProcessor()->getFormElement()}
diff --git a/wcfsetup/install/files/acp/templates/__captchaFormField.tpl b/wcfsetup/install/files/acp/templates/__captchaFormField.tpl
new file mode 100644 (file)
index 0000000..f4202f8
--- /dev/null
@@ -0,0 +1 @@
+{@$field->getObjectType()->getProcessor()->getFormElement()}
index c3f111ac7c3aad618d409bc0846d19f0c3201bed..153ffac4d8871caeb9aa47e9d2c3f7e05079a43a 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 namespace wcf\system\form\builder\field;
 use wcf\system\captcha\ICaptchaHandler;
+use wcf\system\exception\UserInputException;
+use wcf\system\form\builder\field\validation\FormFieldValidationError;
 use wcf\system\form\builder\IObjectTypeFormNode;
 use wcf\system\form\builder\TObjectTypeFormNode;
 
@@ -19,6 +21,17 @@ class CaptchaFormField extends AbstractFormField implements IObjectTypeFormNode
                objectType as defaultObjectType;
        }
        
+       /**
+        * @inheritDoc
+        */
+       protected $templateName = '__captchaFormField';
+       
+       /**
+        * exception thrown by the captcha API during validation
+        * @var null|UserInputException
+        */
+       protected $validationException;
+       
        /**
         * @inheritDoc
         */
@@ -39,11 +52,18 @@ class CaptchaFormField extends AbstractFormField implements IObjectTypeFormNode
        /**
         * @inheritDoc
         */
-       public function getHtml() {
-               /** @var ICaptchaHandler $captcha */
-               $captcha = $this->getObjectType()->getProcessor();
+       public function getHtmlVariables() {
+               $variables = [
+                       'ajaxCaptcha' => $this->getDocument()->isAjax(),
+                       'captchaID' => $this->getPrefixedId()
+               ];
                
-               return $captcha->getFormElement();
+               if ($this->validationException !== null) {
+                       $variables['errorField'] = $this->validationException->getField();
+                       $variables['errorType'] = $this->validationException->getType();
+               }
+               
+               return $variables;
        }
        
        /**
@@ -53,6 +73,13 @@ class CaptchaFormField extends AbstractFormField implements IObjectTypeFormNode
                return 'com.woltlab.wcf.captcha';
        }
        
+       /**
+        * @inheritDoc
+        */
+       public function isAvailable() {
+               return $this->objectType !== null && parent::isAvailable();
+       }
+       
        /**
         * @inheritDoc
         */
@@ -103,7 +130,13 @@ class CaptchaFormField extends AbstractFormField implements IObjectTypeFormNode
                /** @var ICaptchaHandler $captcha */
                $captcha = $this->getObjectType()->getProcessor();
                
-               $captcha->validate();
+               try {
+                       $captcha->validate();
+               }
+               catch (UserInputException $e) {
+                       $this->validationException = $e;
+                       $this->addValidationError(new FormFieldValidationError($e->getType()));
+               }
        }
        
        /**