symfony/symfony — Issue #2605: [Form][Validation] Form error messages duplicated with inheritance
Author: benjamindulau
Created: 2011-11-10 17:02:37+00:00
State: open

# Use case

## Models:

abstract class AbstractUser
{
private $username;
private $password;
}

class ConcreteUser extends AbstractUser
{
/**
* @var UserProfile
*/
private $profile;
}

class UserProfile
{
private $name;
}

## validation.xml (1)

Registration

Registration

## validation.xml (2) :

Profile
Registration

When setting _validation_groups_ to `Registration`, the error messages show twice for the `UserProfile::name` property.

A simple test can be adding the username validation again into `ConcreteUser` class constraints :

Registration

Then validation error messages will show twice on the _username_ as well.

In production environnement that's inconvenient :/

# Hack, ugly but....

For now, there is a hack for the ones having the same issue, in form theme blocks (twig) :

{% block field_errors %}
{% spaceless %}
{% if errors|length > 0 %}

{% set lastError = null %}
{% for error in errors %}
{% if lastError != error %}

{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}

{% endif %}
{% set lastError = error %}
{% endfor %}

{% endif %}
{% endspaceless %}
{% endblock field_errors %}