gpt4 book ai didi

validation - TYPO3 9,将表单验证错误分配给相应的字段

转载 作者:行者123 更新时间:2023-12-03 08:43:09 25 4
gpt4 key购买 nike

通常,TYPO3在FrontEnd中分配一个名称为validationResults的变量,它看起来像那样。
Errors
然后,我们必须遍历Error对象,并获得包含所有错误及其消息的列表。但是没有办法将每个单独地分配给出现错误的相应字段。
所以问题是,我该怎么办?
最好的祝福,

最佳答案

经过一些编程和来自larry-pete(typo3.slack.com)的帮助,我找到了解决方案。

我创建了自己的ViewHelper ,它看起来像这样:

ext_localconf.php

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

类/ViewHelpers/表格/ErrorViewHelper.php
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"/>

并返回:

Results

因此,现在您可以执行以下操作:
<f:if condition="{offer.firstName}"><span style="display: block">{offer.firstName.message}</span></f:if>

最好的祝福

关于validation - TYPO3 9,将表单验证错误分配给相应的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59898387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com