Validation 键入3 9,将表单验证错误分配到相应字段

Validation 键入3 9,将表单验证错误分配到相应字段,validation,error-handling,typo3,fluid,extbase,Validation,Error Handling,Typo3,Fluid,Extbase,通常情况下,TYPO3在前端分配一个名为validationResults的变量,看起来是这样的 然后我们必须遍历错误对象,并获得包含所有错误及其消息的列表。但是,没有办法将每一个单独地分配到出现错误的相应字段中 所以问题是,我该怎么做 致以最诚挚的问候,经过一些编程和拉里·皮特(typo3.slack.com)的帮助,我找到了一个解决方案 我创建了自己的ViewHelper,它看起来如下所示: ext\u localconf.php $GLOBALS['TYPO3_CONF_VARS']['

通常情况下,TYPO3在前端分配一个名为
validationResults
的变量,看起来是这样的

然后我们必须遍历错误对象,并获得包含所有错误及其消息的列表。但是,没有办法将每一个单独地分配到出现错误的相应字段中

所以问题是,我该怎么做


致以最诚挚的问候,

经过一些编程和拉里·皮特(typo3.slack.com)的帮助,我找到了一个解决方案

我创建了自己的ViewHelper,它看起来如下所示:

ext\u localconf.php

$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['mytag'][] = 'Vendor\\ExtensionName\\ViewHelpers';
namespace Vendor\ExtensionName\ViewHelpers\Form;

use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;


class ErrorViewHelper extends AbstractFormFieldViewHelper
{
  use CompileWithRenderStatic;

  /**
  * As this ViewHelper renders HTML, the output must not be escaped.
  *
  * @var bool
  */
  protected $escapeOutput = false;

  public function initializeArguments()
 {
    $this->registerArgument('for', 'string', 'The name of the error name (e.g. argument name or property name). This can also be a property path (like blog.title), and will then only display the validation errors of that property.', false, '');
    $this->registerArgument('as', 'string', '', false, 'flattenedErrors');
 }


 /**
 * @param array $arguments
 * @param \Closure $renderChildrenClosure
 * @param RenderingContextInterface $renderingContext
 * @return mixed
 */
 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
 {
    $as = $arguments['as'];
    $for = $arguments['for'];

    $templateVariableContainer = $renderingContext->getVariableProvider();
    $controllerContext = $renderingContext->getcontrollerContext();
    $validationResults = $controllerContext->getRequest()->getOriginalRequestMappingResults();

    if ($validationResults !== null){
        $validationResults = $validationResults->forProperty($for);
    }
    $flattenedErrors = $validationResults->getErrors();

    $output = $renderChildrenClosure();
    $withKeys = [];
    if (empty($output)) {
        if ($flattenedErrors) {
            foreach ($flattenedErrors as $error) {
                $withKeys[$error->getTitle()] = $error;
            }
        }
    }
    $templateVariableContainer->add($as, $withKeys);

    return $output;
 }
}
Classes/ViewHelpers/Form/ErrorViewHelper.php

$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['mytag'][] = 'Vendor\\ExtensionName\\ViewHelpers';
namespace Vendor\ExtensionName\ViewHelpers\Form;

use TYPO3\CMS\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper;


class ErrorViewHelper extends AbstractFormFieldViewHelper
{
  use CompileWithRenderStatic;

  /**
  * As this ViewHelper renders HTML, the output must not be escaped.
  *
  * @var bool
  */
  protected $escapeOutput = false;

  public function initializeArguments()
 {
    $this->registerArgument('for', 'string', 'The name of the error name (e.g. argument name or property name). This can also be a property path (like blog.title), and will then only display the validation errors of that property.', false, '');
    $this->registerArgument('as', 'string', '', false, 'flattenedErrors');
 }


 /**
 * @param array $arguments
 * @param \Closure $renderChildrenClosure
 * @param RenderingContextInterface $renderingContext
 * @return mixed
 */
 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
 {
    $as = $arguments['as'];
    $for = $arguments['for'];

    $templateVariableContainer = $renderingContext->getVariableProvider();
    $controllerContext = $renderingContext->getcontrollerContext();
    $validationResults = $controllerContext->getRequest()->getOriginalRequestMappingResults();

    if ($validationResults !== null){
        $validationResults = $validationResults->forProperty($for);
    }
    $flattenedErrors = $validationResults->getErrors();

    $output = $renderChildrenClosure();
    $withKeys = [];
    if (empty($output)) {
        if ($flattenedErrors) {
            foreach ($flattenedErrors as $error) {
                $withKeys[$error->getTitle()] = $error;
            }
        }
    }
    $templateVariableContainer->add($as, $withKeys);

    return $output;
 }
}
Form.html

<mytag:form.error as="error" for="error"/>

这将返回:

现在,你可以这样做:

<f:if condition="{offer.firstName}"><span style="display: block">{offer.firstName.message}</span></f:if>
{offer.firstName.message}

致以最诚挚的问候

我稍微更改了您的Viewhelper,因为它在我的版本(键入3 9)中不起作用

{ff:error( for:"user" ) -> f:variable(name: 'errors')}}