7

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:

  1. This username is already used. Please choose another one.
  2. The username is too short - please enter at least 2 symbols.
  3. The username is too short - please enter at least 2 symbols.

For email:

  1. Please enter a valid email.
  2. Please enter a valid email.

and if I enter already used email, the error is shown only once:

  1. This email is already used.

And for the passwords:

If they are short:

  1. The password is too short - please enter at least 6 symbols.
  2. The password is too short - please enter at least 6 symbols.

And if they don't match:

  1. 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:

  1. 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';
    }
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Faery
  • 4,552
  • 10
  • 50
  • 92

2 Answers2

13

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]
AlterPHP
  • 12,667
  • 5
  • 49
  • 54
  • Thank you very much! :) It's perfect now, only it wasn't working with validation groups written with dashes and with RegistrationAcme, so I changed them to `[Registration, Default]` and `[ProfileAcme, Default]` – Faery Mar 22 '13 at 07:30
  • Finally a good and clear answer to this problem I've been searching a solution to for hours. – Didier Ghys Jun 01 '15 at 20:23
  • Hi @AlterPHP - Do you think you would be able to give me a hand with similar problem described [here](http://stackoverflow.com/questions/34631172/duplicated-password-validation-messages)? I tried something similar to your answer above but failed to solve the problem. Highly likely I did something wrong/missing. – BentCoder Jan 11 '16 at 15:17
0
# config.yml
# ...
fos_user:
    db_driver:     orm
    firewall_name: main
    user_class:    Acme\YourBundle\Entity\User
    registration:
        form:
            type: acme_user_registration
            validation_groups: [Registration]
# ... 

Adding the last line ("validation_groups: [Registration]") results in only one error. Only FOSUserBundle validation: "Registration".

José Martínez
  • 36
  • 1
  • 1
  • 5