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

时间:2016-11-03 11:02:55

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

我有一个 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 个答案:

答案 0 :(得分:6)

我通常会这样定制:

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

答案 1 :(得分: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>

答案 2 :(得分:1)

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

答案 3 :(得分: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&#34;插件&#34;的片段必须改变&#34; config&#34;并且必须转义标签键中的点。

相关问题