[FEATURE] A new eval option in TCA: email 51/33251/17
authorOlle Haerstedt <o.haerstedt@bitmotion.de>
Fri, 10 Oct 2014 12:36:54 +0000 (14:36 +0200)
committerMarkus Klein <klein.t3@reelworx.at>
Wed, 15 Oct 2014 10:24:02 +0000 (12:24 +0200)
Add a new eval option called 'email' to validate e-mail addresses.
If validation fails, a flash error message will be shown.

Resolves: #62147
Releases: master
Change-Id: Ie3885aa55a3fde95832fec163bc9347f06b4f54b
Reviewed-on: http://review.typo3.org/33251
Reviewed-by: Stefan Froemken <froemken@gmail.com>
Tested-by: Stefan Froemken <froemken@gmail.com>
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
typo3/sysext/core/Classes/DataHandling/DataHandler.php
typo3/sysext/core/Documentation/Changelog/master/Feature-62147-NewEmailEvalInTCA.rst [new file with mode: 0644]
typo3/sysext/lang/locallang_core.xlf

index fd120ae..cc347d6 100644 (file)
@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Core\DataHandling;
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Database\DatabaseConnection;
+use TYPO3\CMS\Core\Messaging\FlashMessage;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Versioning\VersionState;
 
@@ -2506,6 +2507,9 @@ class DataHandler {
                                                $value = GeneralUtility::idnaEncode($value);
                                        }
                                        break;
+                               case 'email':
+                                       $this->checkValue_input_ValidateEmail($value, $set);
+                                       break;
                                default:
                                        $evalObj = GeneralUtility::getUserObj($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tce']['formevals'][$func] . ':&' . $func);
                                        if (is_object($evalObj) && method_exists($evalObj, 'evaluateFieldValue')) {
@@ -2520,6 +2524,35 @@ class DataHandler {
        }
 
        /**
+        * If $value is not a valid e-mail address,
+        * $set will be set to false and a flash error
+        * message will be added
+        *
+        * @param string $value Value to evaluate
+        * @param bool $set TRUE if an update should be done
+        * @throws \InvalidArgumentException
+        * @throws \TYPO3\CMS\Core\Exception
+        * @return void
+        */
+       protected function checkValue_input_ValidateEmail($value, &$set) {
+               if (GeneralUtility::validEmail($value)) {
+                       return;
+               }
+
+               $set = FALSE;
+               /** @var FlashMessage $message */
+               $message = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage',
+                       sprintf($GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:error.invalidEmail'), $value),
+                       '', // header is optional
+                       FlashMessage::ERROR,
+                       TRUE // whether message should be stored in session
+               );
+               /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
+               $flashMessageService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
+               $flashMessageService->getMessageQueueByIdentifier()->enqueue($message);
+       }
+
+       /**
         * Returns data for group/db and select fields
         *
         * @param array $valueArray Current value array
@@ -7130,7 +7163,7 @@ class DataHandler {
                while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res_log)) {
                        $log_data = unserialize($row['log_data']);
                        $msg = $row['error'] . ': ' . sprintf($row['details'], $log_data[0], $log_data[1], $log_data[2], $log_data[3], $log_data[4]);
-                       $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', htmlspecialchars($msg), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR, TRUE);
+                       $flashMessage = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessage', htmlspecialchars($msg), '', FlashMessage::ERROR, TRUE);
                        /** @var $flashMessageService \TYPO3\CMS\Core\Messaging\FlashMessageService */
                        $flashMessageService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Messaging\\FlashMessageService');
                        /** @var $defaultFlashMessageQueue \TYPO3\CMS\Core\Messaging\FlashMessageQueue */
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-62147-NewEmailEvalInTCA.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-62147-NewEmailEvalInTCA.rst
new file mode 100644 (file)
index 0000000..c3f0edf
--- /dev/null
@@ -0,0 +1,29 @@
+===============================================
+Feature: #62147 - New eval option in TCA: email
+===============================================
+
+Description
+===========
+
+A new option is added to the eval field: email. This will 
+check on server side if entered value is a valid e-mail address.
+If not, a flash error message will be shown.
+
+Usage:
+
+::
+    'email' => array(
+        'exclude' => 1,
+        'label' => 'LLL:EXT:wd_products/Resources/Private/Language/locallang_db.xlf:tx_wdproducts_domain_model_contactperson.email',
+        'config' => array(
+            'type' => 'input',
+            'size' => 30,
+            'eval' => 'email,trim'
+        ),
+    )
+..
+
+Impact
+======
+
+Users don't have to write their own validation classes for e-mail validation.
index 2376128..7a7188a 100644 (file)
@@ -1089,6 +1089,9 @@ Check also the following points:\n
                        <trans-unit id="error.items_proc_func_error" xml:space="preserve">
                                <source>An error occurred trying to process items for field "%1$s" (%2$s).</source>
                        </trans-unit>
+                       <trans-unit id="error.invalidEmail" xml:space="preserve">
+                               <source>"%s" is not a valid e-mail address.</source>
+                       </trans-unit>
                </body>
        </file>
 </xliff>