Rafael Adel
Rafael Adel

Reputation: 7759

Symfony - FOSUserBundle registration validation not working

I'm using FOSUserBundle, I'm trying to validate email and username if they're already exist or not. Otherwise I get MySQL duplicate entry error.

I've extended the registration form, added the Registration validation group. But still no good, I always get duplicate entry error.

Here's my RegistrationType class:

class RegistrationType extends BaseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            "data_class" => "Tsk\FEBundle\Entity\User",
            "validation_groups" => array("Registration")
        ));
    }

    public function getName()
    {
        return "tsk_fe_registration_form_type";
    }
}

And the service definition:

<parameters>
    <parameter key="tsk_fe.registration.form.type.class">Tsk\FEBundle\Form\RegistrationType</parameter>
</parameters>
<services>
    <service id="tsk_fe.registration.form.type" class="%tsk_fe.registration.form.type.class%">
        <tag name="form.type" alias="tsk_fe_registration_form_type"/>
        <argument>%fos_user.model.user.class%</argument>
    </service>
</services>

In config.yml:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Tsk\FEBundle\Entity\User
    registration:
            form:
                type: tsk_fe_registration_form_type

And finally my User entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 * @ORM\HasLifecycleCallbacks
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
    }
    //..
}

Upvotes: 3

Views: 3572

Answers (4)

user2118788
user2118788

Reputation: 129

the problem is with symfony 2.5 validations, change your config.yml from

framework:
    validation:      { enable_annotations: true }

to

framework:
    validation:
        enable_annotations: true
        enabled: true
        api: 2.4

and it will work, it helped for me

Upvotes: 2

jspizziri
jspizziri

Reputation: 793

I was experiencing this same issue until I read through the upgrade doc here https://github.com/symfony/symfony/blob/master/UPGRADE-2.5.md

I explicitly set my validator enabled to true in the config, cleared my cache, and this resolved the issue:

BEFORE

framework:
    validation:      { enable_annotations: true }

AFTER

framework:
    validation:      { enabled: true, enable_annotations: true }

Also, make sure that you are setting both usernameCononical and emailCononical on your entity, as that is what the constraints are against.

I hope this helps.

Upvotes: 1

Debreczeni Andr&#225;s
Debreczeni Andr&#225;s

Reputation: 1678

Alternatively before downgrading Symfony, you may try setting the validation api in you config.yml:

 framework: 
    validation: 
       enabled: true 
       api: auto

That may handle backwards validation. Problems with ValidatorConstraint in Symfony 2.5

Upvotes: 0

Emii Khaos
Emii Khaos

Reputation: 10085

With symfony 2.5.0 the validation API changed and now the storage-specific validation is not loaded. Either downgrade to the latest 2.4 version or simply as workaround, copy the unique constraints to your entity:

use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 * @ORM\HasLifecycleCallbacks
 * 
 * @UniqueEntity(fields="usernameCanonical", errorPath="username", message="fos_user.username.already_used", groups={"Registration", "Profile"})
 * @UniqueEntity(fields="emailCanonical", errorPath="email", message="fos_user.email.already_used", groups={"Registration", "Profile"})
 */
class User extends BaseUser
{
    // your stuff
}

Upvotes: 1

Related Questions