Typo3:如何通过属性验证覆盖默认错误消息?

3

我有一个类Publisher,想要进行属性验证。但是我想要覆盖默认的错误消息。

这里是我Publisher模型的一部分代码片段:

<?php
namespace Typo3\LpSurvey\Domain\Model;

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

class Publisher extends AbstractEntity
{

    /**
     * salutation
     *
     * @var bool
     * @validate NotEmpty
     */
    protected $salutation;

    ...
}

这是我的发布器对象的部分代码:
<div class="container publisher">
    <div class="row">
        <div class="col-sm-12">
            <legend>Anrede <em class="star">*</em></legend>
            // Error message output---------------------
            <f:render partial="FormErrorsPublisher" arguments="{field: 'newSigil.survey.publisher.salutation'}" />
            //------------------------------------------
            <label class="label-radio">
                <f:form.radio value="0" property="survey.publisher.salutation" />
                Frau
            </label>
            <label class="label-radio">
                <f:form.radio value="1" property="survey.publisher.salutation" />
                Herr
            </label>
        </div>
    </div>
    ...
</div>

这是我的FormErrorsPublisher部分视图(也是一个片段):

<f:form.validationResults for="{field}">
    <f:if condition="{validationResults.flattenedErrors}">
        <f:for each="{validationResults.flattenedErrors}" as="errors">
            <ul class="error-field">
                <f:for each="{errors}" as="error">
                    <li class="error">
                        {error}
                    </li>
                </f:for>
            </ul>
        </f:for>
    </f:if>
</f:form.validationResults>

如果称谓字段为空,我会收到默认的NotEmpty错误消息,但我想覆盖它。

也许在locallang.xlf文件中使用错误代码?

我尝试了这个方法,但没有解决方案:

<xliff version="1.0">
    <file source-language="en" datatype="plaintext" original="messages" date="2016-10-06T09:49:41Z" product-name="lp_survey">
        <header/>
        <body>
            ...
            <trans-unit id="survey.publisher.salutation.1221560910">
                <source>Der angegebene Wert ist leer.</source>
            </trans-unit>
        </body>
    </file>
</xliff>

有人有想法吗?

4个回答

7

我通常像这样进行定制:

<f:form.validationResults for="{field}">
    <f:for each="{validationResults.flattenedErrors}" key="propertyPath" as="propertyErrors">
        <f:for each="{propertyErrors}" as="propertyError">
            <div class="form__field-error">
                <f:translate key="validator.{propertyPath}.{propertyError.code}" default="{propertyError}" />
            </div>
        </f:for>
    </f:for>
</f:form.validationResults>

然后locallang.xlf可以包含重写的验证错误消息(在下面的情况下,它是RegExp验证器的错误代码):

<trans-unit id="validator.object.property.1221565130">
    <source>Input doesn't match the regexp.</source>
</trans-unit>

上述结构可以不带 for 参数使用,仍能正常工作。


3

若想要更全面的解决方案,您可以在 TypoScript 中简单更改 extbase 的翻译。这样,您就不需要在模板中进行任何操作,只需像以前一样显示错误消息即可。此更改将影响您所有的模板和其他 extbase 扩展,因此您将获得一个整体应用流程的简洁错误消息。

plugin.tx_extbase._LOCAL_LANG.de {
  validator.notempty.empty = Der angegebene Wert ist leer.
  validator.notempty.null = Der angegebene Wert ist leer.
}

作为额外的奖励,我已经添加了 `validator.notempty.null`,因为对于大多数终端用户来说,NULL 错误消息没有意义。
更新
我的 FormPropertyError 部分如下所示 - 但是您的片段也应该有效。
<f:comment>
    Only required parameter is {property}
</f:comment>
<f:form.validationResults for="{property}">
    <f:if condition="{validationResults.errors}">
        <ul class="errors">
            <f:for each="{validationResults.errors}" as="error">
                <li>{error.message -> f:format.printf(arguments: error.arguments)}</li>
            </f:for>
        </ul>
    </f:if>
</f:form.validationResults>

嗨,这听起来也很棒,但我无法运行它。我将您的代码添加到我的模板设置区域中,但我仍然收到默认错误消息。 顺便说一句,我的实际直觉是覆盖NULL消息。 :) - Felix
嗯,这很奇怪。这是从一个正常工作的TYPO3 7.6安装中复制粘贴的。我唯一更改的是翻译本身和将“.da”更改为“ .de”,以代替丹麦语的德语。我不会说德语,但这是您要更改翻译的语言对吧? - Lasse
嗯,它应该可以工作,我不知道为什么对你来说不起作用。唯一我能看到我们之间的区别是消息的输出方式。我会在我的答案中更新我的FormError部分。 - Lasse
你的更新部分已经可以正常工作了。:) 感谢你提供的解决方案! - Felix

2
自TYPO3 6.2版本以来,您可以使用自定义翻译文件覆盖Extbase(或任何其他扩展)的默认验证消息。
在网站包或提供者扩展的ext_localconf.php中,您需要定义要覆盖的语言文件。下面的示例覆盖了德语语言文件中的Extbase本地化文件。
$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['de']['EXT:extbase/Resources/Private/Language/locallang.xlf'][] = 'EXT:your_sitepackage/Resources/Private/Language/de.extbase_locallang.xlf';

这定义了对语言文件 de.extbase_locallang.xlf 的引用,从中覆盖原始 XLIFF 文件的本地化。

自定义语言文件中的示例内容:

  <trans-unit id="validator.boolean.nottrue">
    <source>The given subject was not true.</source>
    <target>Sie müssen dieses Feld bestätigen.</target>
  </trans-unit>

0

TypoScript的方式是

config.tx_extbase._LOCAL_LANG.de {
  validator\.notempty\.empty = Der angegebene Wert ist leer.
  validator\.notempty\.null = Der angegebene Wert ist leer.
}

在Lasse的代码片段中,“plugin”必须更改为“config”,并且标签键中的点号必须进行转义。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接