[TASK] Add validation for form object 11/37811/2 master
authorSebastian Fischer <typo3@evoweb.de>
Fri, 13 Mar 2015 09:41:32 +0000 (10:41 +0100)
committerSebastian Fischer <typo3@evoweb.de>
Fri, 13 Mar 2015 09:47:57 +0000 (10:47 +0100)
Change-Id: I2f84e6a9658f93fc05816f77cca14faccd3bdda6
Reviewed-on: http://review.typo3.org/37811
Reviewed-by: Sebastian Fischer <typo3@evoweb.de>
Tested-by: Sebastian Fischer <typo3@evoweb.de>
Classes/Adapter/TypoScriptAdapter.php
Classes/Validation/RecaptchaValidator.php [new file with mode: 0644]
ext_autoload.php [new file with mode: 0644]

index 149c505..e9ec0d9 100644 (file)
@@ -23,6 +23,9 @@ namespace Evoweb\Recaptcha\Adapter;
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
+
 /**
  * Class RecaptchaAdapter
  */
@@ -35,10 +38,10 @@ class TypoScriptAdapter {
        protected $captcha = NULL;
 
        /**
-        * Constructor
+        * @return self
         */
        public function __construct() {
-               $this->captcha = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Evoweb\\Recaptcha\\Services\\CaptchaService');
+               $this->captcha = GeneralUtility::makeInstance('Evoweb\\Recaptcha\\Services\\CaptchaService');
        }
 
        /**
@@ -49,6 +52,17 @@ class TypoScriptAdapter {
        public function render() {
                if ($this->captcha !== NULL) {
                        $output = $this->captcha->getReCaptcha();
+
+                       /** @var \TYPO3\CMS\Form\Validation\RecaptchaValidator $recaptchaValidator */
+                       $recaptchaValidator = GeneralUtility::makeInstance('TYPO3\\CMS\\Form\\Validation\\RecaptchaValidator');
+                       $validationError = $recaptchaValidator->getError();
+                       if (count($validationError)) {
+                               /** @var ContentObjectRenderer $content */
+                               $content = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
+
+                               $output .= '<strong class="error">' . $content->cObjGetSingle($validationError['cObj'], $validationError['cObj.']) .
+                                       '</strong>';
+                       }
                } else {
                        $output = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
                                'error_captcha.notinstalled', 'Recaptcha', array('recaptcha')
@@ -57,62 +71,4 @@ class TypoScriptAdapter {
 
                return $output;
        }
-
-       /**
-        * Validate the captcha value from the request and output an error if not valid
-        *
-        * @return bool
-        */
-       public function validate() {
-               $validCaptcha = TRUE;
-
-               if ($this->captcha !== NULL) {
-                       $status = $this->captcha->validateReCaptcha();
-
-                       if ($status == FALSE || $status['error'] !== '') {
-                               $validCaptcha = FALSE;
-                               $this->renderFlashMessage(
-                                       \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('error_recaptcha_' . $status['error'], 'Recaptcha'),
-                                       1307421960
-                               );
-                       }
-               }
-
-               return $validCaptcha;
-       }
-
-       /**
-        * @param string $message
-        * @param int $type
-        * @throws \TYPO3\CMS\Core\Exception
-        */
-       protected function renderFlashMessage($message, $type = \TYPO3\CMS\Core\Messaging\FlashMessage::WARNING) {
-               $code  = '
-               .typo3-message .message-header{padding: 10px 10px 0 30px;font-size:0.9em;}
-               .typo3-message .message-body{padding: 10px;font-size:0.9em;}
-               ';
-
-               /**
-                * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $frontend
-                */
-               $frontend = $GLOBALS['TSFE'];
-               $frontend->getPageRenderer()->addCssFile(
-                       \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::siteRelPath('t3skin') . 'stylesheets/standalone/errorpage-message.css'
-               );
-               $frontend->getPageRenderer()->addCssInlineBlock('flashmessage', $code);
-
-               /** @var \TYPO3\CMS\Core\Messaging\FlashMessage $flashMessage */
-               $flashMessage = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
-                       'TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
-                       $message,
-                       '',
-                       $type
-               );
-
-               /** @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue $flashMessageQueue */
-               $flashMessageQueue = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
-                       'TYPO3\\CMS\\Core\\Messaging\\FlashMessageQueue'
-               );
-               $flashMessageQueue->enqueue($flashMessage);
-       }
 }
diff --git a/Classes/Validation/RecaptchaValidator.php b/Classes/Validation/RecaptchaValidator.php
new file mode 100644 (file)
index 0000000..bb43240
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+namespace TYPO3\CMS\Form\Validation;
+
+/**
+ * Class RecaptchaValidator
+ */
+class RecaptchaValidator extends AbstractValidator implements \TYPO3\CMS\Core\SingletonInterface {
+       /**
+        * Captcha object
+        *
+        * @var \Evoweb\Recaptcha\Services\CaptchaService
+        */
+       protected $captcha = NULL;
+
+       /**
+        * @param array $arguments
+        * @return self
+        */
+       public function __construct($arguments) {
+               parent::__construct($arguments);
+
+               $this->captcha = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Evoweb\\Recaptcha\\Services\\CaptchaService');
+       }
+
+       /**
+        * Validate the captcha value from the request and output an error if not valid
+        *
+        * @return bool
+        */
+       public function isValid() {
+               $validCaptcha = TRUE;
+
+               if ($this->captcha !== NULL) {
+                       $status = $this->captcha->validateReCaptcha();
+
+                       if ($status == FALSE || $status['error'] !== '') {
+                               $validCaptcha = FALSE;
+                               $this->setError($status['error']);
+                       }
+               }
+
+               return $validCaptcha;
+       }
+}
\ No newline at end of file
diff --git a/ext_autoload.php b/ext_autoload.php
new file mode 100644 (file)
index 0000000..c37eb68
--- /dev/null
@@ -0,0 +1,7 @@
+<?php
+
+$extensionPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('recaptcha');
+
+return array(
+       'TYPO3\\CMS\\Form\\Validation\\RecaptchaValidator' => $extensionPath . 'Classes/Validation/RecaptchaValidator.php',
+);
\ No newline at end of file