【问题标题】:Typo3: How can I overwrite the default error message by property validation?Typo3:如何通过属性验证覆盖默认错误消息?
【发布时间】:2017-03-16 22:34:33
【问题描述】:

我有一个类 Publisher,我想通过属性验证对其进行验证。 但是我想覆盖默认的错误信息。

这是我的 Publisher 模型的 sn-p:

<?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 部分(也是一个 sn-p):

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

有人有想法吗?

【问题讨论】:

    标签: php validation typo3 extbase typo3-7.6.x


    【解决方案1】:

    我通常是这样自定义的:

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

    【讨论】:

      【解决方案2】:

      要获得更全球化的解决方案,您可以在 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 部分如下所示 - 但您的 sn-p 应该也能正常工作。

      <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 消息。 :)
      • 嗯,这很奇怪。这是一个工作 TYPO3 7.6 安装的复制粘贴。我唯一改变的是它自己的翻译,以及德语而不是丹麦语的“.da”到“.de”的翻译。我不会说德语,但那是您尝试更改翻译的语言,对吗?
      • 嗯,它应该工作,我不知道它不适合你。我唯一能看到我们做的不同的是消息的输出。我将用我的 FormError 部分更新我的答案
      • 好的,您更新的部分内容也适用于我。 :) 感谢您的解决方案!
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        TypoScript 方式是

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

        在 Lasse 的 sn-p 中,“plugin”必须在“config”中更改,并且标签键中的点必须转义。

        【讨论】:

          猜你喜欢
          • 2020-01-17
          • 1970-01-01
          • 1970-01-01
          • 2014-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-13
          相关资源
          最近更新 更多