[BUGFIX] Validation error arguments are not applied 43/56743/2
authorRalf Zimmermann <ralf.zimmermann@tritum.de>
Sat, 17 Mar 2018 16:50:26 +0000 (17:50 +0100)
committerFrank Naegler <frank.naegler@typo3.org>
Thu, 19 Apr 2018 18:10:42 +0000 (20:10 +0200)
If an extbase error object wants to be translated through EXT:form and
no translation is found, the original error message will be shown.
Possibly existing error message arguments must be applied by calling
the error objects __toString() method.

Resolves: #84449
Releases: master, 8.7
Change-Id: I3d1f92a7d9c9abcc239bdab17b42a2cbb9e98d94
Reviewed-on: https://review.typo3.org/56328
Reviewed-by: Björn Jacob <bjoern.jacob@tritum.de>
Tested-by: Björn Jacob <bjoern.jacob@tritum.de>
Reviewed-by: Kay Strobach <typo3@kay-strobach.de>
Tested-by: Kay Strobach <typo3@kay-strobach.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
Reviewed-on: https://review.typo3.org/56743

typo3/sysext/core/Documentation/Changelog/8.7.x/Deprecation-84449-TranslateElementErrorViewHelperArguments.rst [new file with mode: 0644]
typo3/sysext/form/Classes/ViewHelpers/TranslateElementErrorViewHelper.php
typo3/sysext/form/Resources/Private/Frontend/Partials/Field/Field.html

diff --git a/typo3/sysext/core/Documentation/Changelog/8.7.x/Deprecation-84449-TranslateElementErrorViewHelperArguments.rst b/typo3/sysext/core/Documentation/Changelog/8.7.x/Deprecation-84449-TranslateElementErrorViewHelperArguments.rst
new file mode 100644 (file)
index 0000000..1b0d7cb
--- /dev/null
@@ -0,0 +1,47 @@
+.. include:: ../../Includes.txt
+
+===============================================================
+Deprecation: #84449 - TranslateElementErrorViewHelper arguments
+===============================================================
+
+See :issue:`84449`
+
+Description
+===========
+
+The template EXT:form/Resources/Private/Frontend/Partials/Field/Field.html has been changed. This
+was necessary because of a bug with validation messages containing arguments.
+For compatibility reasons, the old template syntax is still supported but is deprecated and will be
+removed with TYPO3 v10.
+
+Impact
+======
+
+If a user utilizes his own template for EXT:form/Resources/Private/Frontend/Partials/Field/Field.html,
+a deprecation warning will be thrown.
+
+
+Affected Installations
+======================
+
+Any installation which uses the form framework and a customized template for
+EXT:form/Resources/Private/Frontend/Partials/Field/Field.html.
+
+
+Migration
+=========
+
+Change
+
+.. code-block:: html
+
+   {formvh:translateElementError(element: element, code: error.code, arguments: error.arguments, defaultValue: error.message)}
+
+to
+
+.. code-block:: html
+
+   {formvh:translateElementError(element: element, error: error)}
+
+
+.. index:: Frontend, ext:form
\ No newline at end of file
index 9d839f8..8fe62f0 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Form\ViewHelpers;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Extbase\Validation\Error;
 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
@@ -41,9 +42,10 @@ class TranslateElementErrorViewHelper extends AbstractViewHelper
     {
         parent::initializeArguments();
         $this->registerArgument('element', RootRenderableInterface::class, 'Form Element to translate', true);
-        $this->registerArgument('code', 'integer', 'Error code', true);
-        $this->registerArgument('arguments', 'array', 'Error arguments', false, null);
-        $this->registerArgument('defaultValue', 'string', 'The default value', false, '');
+        $this->registerArgument('error', Error::class, '', false, '');
+        $this->registerArgument('code', 'integer', 'Error code - deprecated', false);
+        $this->registerArgument('arguments', 'array', 'Error arguments - deprecated', false, null);
+        $this->registerArgument('defaultValue', 'string', 'The default value - deprecated', false, '');
     }
 
     /**
@@ -58,17 +60,28 @@ class TranslateElementErrorViewHelper extends AbstractViewHelper
     public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
     {
         $element = $arguments['element'];
+        $error = $arguments['error'];
+
+        $code = $arguments['code'];
+        $errorArguments = $arguments['arguments'];
+        $defaultValue = $arguments['defaultValue'];
+
+        if ($error instanceof Error) {
+            $code = $error->getCode();
+            $errorArguments = $error->getArguments();
+            $defaultValue = $error->__toString();
+        }
 
         /** @var FormRuntime $formRuntime */
-        $formRuntime =  $renderingContext
+        $formRuntime = $renderingContext
             ->getViewHelperVariableContainer()
             ->get(RenderRenderableViewHelper::class, 'formRuntime');
 
         return TranslationService::getInstance()->translateFormElementError(
             $element,
-            $arguments['code'],
-            $arguments['arguments'],
-            $arguments['defaultValue'],
+            $code,
+            $errorArguments,
+            $defaultValue,
             $formRuntime
         );
     }
index 3b01de7..2f993fe 100644 (file)
@@ -10,7 +10,7 @@
                                <f:if condition="{validationResults.flattenedErrors}">
                                        <span class="error help-block" role="alert">
                                                <f:for each="{validationResults.errors}" as="error">
-                                                       {formvh:translateElementError(element: element, code: error.code, arguments: error.arguments, defaultValue: error.message)}
+                                                       {formvh:translateElementError(element: element, error: error)}
                                                        <br />
                                                </f:for>
                                        </span>