Search code examples
phpvalidationtypo3extbasetypo3-7.6.x

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


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?


Solution

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