3

I have a class Publisher, which I want to validate with property validation. But I want to overwrite the default error messages.

Here is a snippet from my Publisher model:

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

use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

class Publisher extends AbstractEntity
{

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

    ...
}

Here is my partial for my publisher object:

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

And here my FormErrorsPublisher partial (also a snippet):

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

Now if the salutation field is empty I get the default NotEmpty error message, but I want to overwrite this.

Maybe in the locallang.xlf with the error code?

I try this, but no solution:

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

Have anyone an idea?

Felix
  • 231
  • 2
  • 17

4 Answers4

7

I'm usually customizing it like this:

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

And then locallang.xlf can contain overridden validation error messages (in case below it is an error code of RegExp validator):

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

The construction above can be used without for argument and will function the same.

Viktor Livakivskyi
  • 3,178
  • 1
  • 21
  • 33
3

For a more global solution you can simple change the translation for extbase in TypoScript. Then you don´t have to do anything in your template but show the error message like you already does. This change will effect all your templates and other extbase extensions as well so you will get a nice streamline error message across your whole application.

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

As a little bonus i have added the validator.notempty.null since the NULL error message doesn't make sense for must end users.

Update

My FormPropertyError partial looks like below - but your snippet should work as well.

<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>
Lasse
  • 575
  • 3
  • 14
  • Hi this sounds also greate, but I dont get it to run. I added your code into my setup area of my template, but I also get the default error message. BTW my actual intuition was to overwrite the NULL message. :) – Felix Nov 04 '16 at 07:44
  • Hmmm, that's weird. It´s a copy paste from a working TYPO3 7.6 installation. The only things i have changes is the translation it self and ´.da´ to ´.de´ for german instead of danish. I do not speech german but that is the language you are trying to change the translation for right? – Lasse Nov 04 '16 at 15:32
  • Well, it should work, I don´t know way it´s doesn´t work for you. The only thing i can see that we do different is the output off the message. I will update my answer with my FormError partial – Lasse Nov 04 '16 at 15:51
  • Ok with your updated partial it works for me too. :) Thx for the solution! – Felix Nov 07 '16 at 08:59
2

Since TYPO3 6.2, you can override the default validation messages from Extbase (or any other extension) using a custom translation file.

In ext_localconf.php of a sitepackage or provider extension, you need to define, which language file to override. The example below overrides the german language file the Extbase localizations.

$GLOBALS['TYPO3_CONF_VARS']['SYS']['locallangXMLOverride']['de']['EXT:extbase/Resources/Private/Language/locallang.xlf'][] = 'EXT:your_sitepackage/Resources/Private/Language/de.extbase_locallang.xlf';

This defines a reference to the language file de.extbase_locallang.xlf, from where localizations for the original XLIFF file will be overridden.

Example Content in the custom language file:

  <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>
derhansen
  • 5,585
  • 1
  • 19
  • 29
0

The TypoScript way is

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

In the snippet from Lasse "plugin" has to changed in "config" and the dots in the label keys have to be escaped.

Maba
  • 1