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

Typo3: How can I overwrite the default error message by property validation?

我有一个 class 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>

有人知道吗?

我通常是这样定制的:

<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 参数的情况下使用,并且功能相同。

对于更全局的解决方案,您可以简单地更改 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 错误消息对 must 最终用户没有意义。

更新

我的 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>

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" 中进行更改,并且必须转义标签键中的点。

从 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>