[TASK] Overwrite mandatory validation for hidden fields
authorOliver Eglseder <oliver.eglseder@in2code.de>
Thu, 3 Sep 2015 12:33:34 +0000 (14:33 +0200)
committerOliver Eglseder <oliver.eglseder@in2code.de>
Thu, 3 Sep 2015 12:33:34 +0000 (14:33 +0200)
Classes/Controller/ConditionController.php
Classes/Domain/Model/Condition.php
Classes/Domain/Validator/ConditionAwareValidator.php [new file with mode: 0644]
Resources/Public/JavaScript/PowermailCondition.js
ext_localconf.php

index 8881405..2d6838b 100644 (file)
@@ -7,6 +7,7 @@ use In2code\Powermail\Domain\Model\Page;
 use In2code\PowermailCond\Domain\Model\ConditionContainer;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 
 /***************************************************************
  *  Copyright notice
@@ -73,8 +74,20 @@ class ConditionController extends ActionController {
                                }
                        }
                }
+
                $conditionContainer = new ConditionContainer($this->conditionRepository->findByForm($form));
                $arguments = $conditionContainer->applyConditions($form, $arguments);
+
+               /** @var TypoScriptFrontendController $feUser */
+               $feUser = GeneralUtility::makeInstance(
+                       'TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController',
+                       $GLOBALS['TYPO3_CONF_VARS'],
+                       0,
+                       0
+               );
+               $feUser->initFEuser();
+               $feUser->fe_user->setAndSaveSessionData('tx_powermail_cond', $arguments);
+
                return json_encode($arguments);
        }
 }
index 6cec52e..c610610 100644 (file)
@@ -47,7 +47,7 @@ class Condition extends AbstractEntity {
        /**
         * @var array
         */
-       protected $actionNumberMap = array(
+       protected static $actionNumberMap = array(
                self::ACTION_HIDE => 'hide',
                self::ACTION_UN_HIDE => 'un_hide',
        );
@@ -266,9 +266,20 @@ class Condition extends AbstractEntity {
                        }
                }
                if ($affectedFieldMarker !== '') {
-                       $arguments['todo'][$affectedFieldMarker] = $this->actionNumberMap[$this->actions];
+                       $arguments['todo'][$affectedFieldMarker] = self::$actionNumberMap[$this->actions];
                }
                return $arguments;
        }
 
+       /**
+        * @param int|NULL $action
+        * @return array
+        */
+       static public function getActionNumberMap($action = NULL) {
+               if ($action !== NULL) {
+                       return self::$actionNumberMap[$action];
+               }
+               return self::$actionNumberMap;
+       }
+
 }
diff --git a/Classes/Domain/Validator/ConditionAwareValidator.php b/Classes/Domain/Validator/ConditionAwareValidator.php
new file mode 100644 (file)
index 0000000..9a7049b
--- /dev/null
@@ -0,0 +1,142 @@
+<?php
+namespace In2code\PowermailCond\Domain\Validator;
+
+use In2code\Powermail\Domain\Model\Field;
+use In2code\Powermail\Domain\Validator\InputValidator;
+use In2code\PowermailCond\Domain\Model\Condition;
+use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
+
+/**
+ * Class ConditionAwareValidator
+ */
+class ConditionAwareValidator extends InputValidator {
+
+       /**
+        * Validate a single field
+        *
+        * @param Field $field
+        * @param mixed $value
+        * @return void
+        */
+       protected function isValidField(Field $field, $value) {
+
+               /** @var FrontendUserAuthentication $feUser */
+               $feUser = $GLOBALS['TSFE']->fe_user;
+               $arguments = $feUser->getSessionData('tx_powermail_cond');
+               $fieldMarker = $field->getMarker();
+               if (!empty($arguments['todo'][$fieldMarker])) {
+                       if (!$arguments['todo'][$fieldMarker] === Condition::getActionNumberMap(Condition::ACTION_HIDE)) {
+                               // Mandatory Check
+                               if ($field->getMandatory()) {
+                                       if (!$this->validateMandatory($value)) {
+                                               $this->setErrorAndMessage($field, 'mandatory');
+                                       }
+                               }
+                       }
+               }
+
+               // String Checks
+               if (!empty($value) && in_array($field->getType(), $this->validationFieldTypes)) {
+                       switch ($field->getValidation()) {
+
+                               // email
+                               case 1:
+                                       if (!$this->validateEmail($value)) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               // URL
+                               case 2:
+                                       if (!$this->validateUrl($value)) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               // phone
+                               case 3:
+                                       if (!$this->validatePhone($value)) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               // numbers only
+                               case 4:
+                                       if (!$this->validateNumbersOnly($value)) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               // letters only
+                               case 5:
+                                       if (!$this->validateLettersOnly($value)) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               // min number
+                               case 6:
+                                       if (!$this->validateMinNumber($value, $field->getValidationConfiguration())) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               // max number
+                               case 7:
+                                       if (!$this->validateMaxNumber($value, $field->getValidationConfiguration())) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               // range
+                               case 8:
+                                       if (!$this->validateRange($value, $field->getValidationConfiguration())) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               // length
+                               case 9:
+                                       if (!$this->validateLength($value, $field->getValidationConfiguration())) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               // pattern
+                               case 10:
+                                       if (!$this->validatePattern($value, $field->getValidationConfiguration())) {
+                                               $this->setErrorAndMessage($field, 'validation.' . $field->getValidation());
+                                       }
+                                       break;
+
+                               /**
+                                * E.g. Validation was extended with Page TSconfig
+                                *              tx_powermail.flexForm.validation.addFieldOptions.100 = New Validation
+                                *
+                                * Register your Class and Method with TypoScript Setup
+                                *              plugin.tx_powermail.settings.setup.validation.customValidation.100 =
+                                *                      In2code\Powermailextended\Domain\Validator\ZipValidator
+                                *
+                                * Add method to your class
+                                *              validate100($value, $validationConfiguration)
+                                *
+                                * Define your Errormessage with TypoScript Setup
+                                *              plugin.tx_powermail._LOCAL_LANG.default.validationerror_validation.100 =
+                                *                      Error happens!
+                                */
+                               default:
+                                       if ($field->getValidation()) {
+                                               $validation = $field->getValidation();
+                                               if (!empty($this->settings['validation.']['customValidation.'][$validation])) {
+                                                       $extendedValidator = $this->objectManager->get($this->settings['validation.']['customValidation.'][$validation]);
+                                                       if (method_exists($extendedValidator, 'validate' . ucfirst($validation))) {
+                                                               if (!$extendedValidator->{'validate' . ucfirst($validation)}($value, $field->getValidationConfiguration())) {
+                                                                       $this->setErrorAndMessage($field, 'validation.' . $validation);
+                                                               }
+                                                       }
+                                               }
+                                       }
+                       }
+               }
+       }
+}
index 391853c..90f6c41 100644 (file)
@@ -46,6 +46,21 @@ function PowermailCondition($formElement) {
                        processData: false,
                        success: function(data) {
                                console.log(data);
+                               if (data.todo !== undefined) {
+                                       var form = $('.powermail_form_' + data.mail.form)
+                                       for (var key in data.todo) {
+                                               var input = form.find('#powermail_field_' + key);
+                                               if (data.todo[key] === 'hide') {
+                                                       input.val('');
+                                                       input.prop('disabled', true);
+                                                       input.closest('.powermail_fieldwrap').hide();
+                                               }
+                                               if (data.todo[key] === 'un_hide') {
+                                                       input.prop('disabled', false);
+                                                       input.closest('.powermail_fieldwrap').show();
+                                               }
+                                       }
+                               }
                        }
                });
        };
index 15713f2..960615c 100644 (file)
@@ -15,4 +15,8 @@ if (!defined ('TYPO3_MODE')) {
        array(
                'Condition' => 'buildCondition'
        )
-);
\ No newline at end of file
+);
+
+$GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects']['In2code\\Powermail\\Domain\\Validator\\InputValidator'] = array(
+       'className' => 'In2code\\PowermailCond\\Domain\\Validator\\ConditionAwareValidator',
+);