Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    }
    //..
}
like image 328
Rafael Adel Avatar asked Feb 28 '26 20:02

Rafael Adel


1 Answers

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

like image 142
user2118788 Avatar answered Mar 02 '26 10:03

user2118788



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!