0
votes

I am using Symfony2 and the FOSUserBundle.

If I enter a username or an email address in the registration form, which is already in the database, an error at the top of the registration form is shown.

How do I put the error message to the username/email field?

2

2 Answers

0
votes

Read documentation Rendering each Field by Hand

{{form_errors(form.username)}}
{{form_errors(form.)}}

or create your form theming Global Form Theming

0
votes

If you set FOSUserBundle as your bundle's parent:

// src/Acme/UserBundle/AcmeUserBundle.php

<?php

namespace Acme\UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AcmeUserBundle extends Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

Then you can place your own validation.yml file in the config folder, and you can set the errorPath property of the unique constraint like this:

#validation.yml:
Acme\DemoBundle\Entity\User:      # your user entity
constraints:
    - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: { fields: usernameCanonical, errorPath: username, groups: [CustomRegistration, Default] }

Make sure you set errorPath as the actual field's name on your form.