Validation of a form - I'm getting the labels two times
forms, symfony, twig, validation
Solution
Ok, this is a known issue.
Let's have a look here : https://github.com/symfony/symfony/issues/2605
The solution is : create your own validation group for properties' validation rules you want to override. In your validation.xml, put only properties you want some different rules, and define validation on a new validation group.
So, for validation.xml, to modify only plainPassword validation rules for your entity (Acme\MyBundle\Entity\User) :
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="Acme\MyBundle\Entity\User">
<property name="plainPassword">
<constraint name="NotBlank">
<option name="message">fos_user.password.blank</option>
<option name="groups">Registration</option>
</constraint>
<constraint name="Length">
<option name="min">6</option>
<option name="minMessage">fos_user.password.short</option>
<option name="groups">
<value>RegistrationAcme</value>
<value>ProfileAcme</value>
</option>
</constraint>
</property>
</class>
</constraint-mapping>
And now you have to state that you use different validation groups for the impacted forms (registration and profile). Fortunately, FOSUserBundle is a good practices' example and allows you to override them in your config.yml :
fos_user:
registration:
form:
validation_groups: [Default, RegistrationAcme]
profile:
form:
validation_groups: [Default, ProfileAcme]
Problem
I have a registration form - from FOSUserBundle this is in the template: ``` {% form_theme form 'AcmeMyBundle:Form:errors.html.twig' %} <form class="big-spacer" action="{{ path('fos_user_registration_register') }}" {{ form_enctype(form) }} method="POST" class="fos_user_registration_register"> {{ form_widget(form) }} <div> <input class="btn little-spacer" type="submit" value="{{ 'registration.submit'|trans({}, 'FOSUserBundle') }}" /> </div> </form> ``` Here is `errors.html.twig`: ``` {% block field_errors %} {% spaceless %} {% if errors|length > 0 %} <ul class="little-spacer nav text-error"> {% for error in errors %} <li>{{loop.index}}. {{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li> {% endfor %} </ul> {% endif %} {% endspaceless %} {% endblock field_errors %} ``` I have just added some css classes from TwitterBootstrap. The problem is that I get some of the messages for the validation twice. My form has 4 fields - Username, Email, Password, Confirm Password I tried to break as many validation rules I can and here is the output: For `Username`: - This username is already used. Please choose another one. - The username is too short - please enter at least 2 symbols. - The username is too short - please enter at least 2 symbols. For `email`: - Please enter a valid email. - Please enter a valid email. and if I enter already used email, the error is shown only once: - This email is already used. And for the `passwords`: If they are short: - The password is too short - please enter at least 6 symbols. - The password is too short - please enter at least 6 symbols. And if they don't match: - The entered passwords don't match. Another strange thing is that when I resubmit the form, but it's still not valid, the notice for the length of the password is only one: - The password is too short - please enter at least 6 symbols. and before resubmitting, they were two. Do you have any ideas why some of the errors are displayed twice and how to fix this? Thank you very much in advance! :) UPDATE This is `C:\xampp\htdocs\Project\src\Acme\MyBundle\Entity\User.php` ``` namespace Acme\MyBundle\Entity; use FOS\UserBundle\Entity\User as BaseUser; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="fos_user") */ class User extends BaseUser { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; public function __construct() { parent::__construct(); // your own logic } } ``` And in `C:\xampp\htdocs\Project\app\Resources\FOSUserBundle\translations\` I copied the file `validators.en.yml` and in it and removed the `[-Inf, Inf]` part and changed the messages a bit. I also overrode the validation file - I copied it here: `C:\xampp\htdocs\Project\src\Acme\MyBundle\Resources\config\validation.xml` I changed only the minimum length of the password. Everything else is the same as in the original file. My bundle extends FOSUserBundle: `C:\xampp\htdocs\Project\src\Acme\MyBundle\AcmeMyBundle.php` this file contains the following: ``` <?php namespace Acme\BudgetTrackerBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; class AcmeBudgetTrackerBundle extends Bundle { public function getParent() { return 'FOSUserBundle'; } } ```