3

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();
    }
    //..
}
Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
Rafael Adel
  • 7,673
  • 25
  • 77
  • 118

4 Answers4

2

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

user2118788
  • 129
  • 3
  • 5
1

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
}
Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
  • Hmm first of all thanks, I've put those two lines as you said, and set `validation_groups => ['Registration']` as it was. But still I get no validation, noting that I've cleared the cache. Is there any anything to perform for this to activate (i.e. `UniqueEntity` constraints`? – Rafael Adel Jun 07 '14 at 14:30
  • Normally the validation annotations should work, if you haven't disabled then. In the [standard distribution the are enabled](https://github.com/symfony/symfony-standard/blob/2.5/app/config/config.yml#L14). Maybe you have to add `enabled: true` under the `validations` key.. See the [changelog](https://github.com/symfony/symfony/blob/master/UPGRADE-2.5.md#validator). – Emii Khaos Jun 07 '14 at 14:51
  • Yea I have these options enabled in `config.yml`: `validation:{ enabled: true, enable_annotations: true }` – Rafael Adel Jun 07 '14 at 14:54
  • Damn, then a downgrade to the latest 2.4 is the simplest solution :P :( – Emii Khaos Jun 07 '14 at 15:03
  • Yea I think so ! But how can I do that without breaking anything ? – Rafael Adel Jun 07 '14 at 15:07
  • Simply `"symfony/symfony": "2.4.*"` in your composer.json. Should be no big deal, if you have not used any new features from 2.5 – Emii Khaos Jun 07 '14 at 15:10
  • Hmmm I will give it a try. Thanks a lot I've taken so much of your time. :) – Rafael Adel Jun 07 '14 at 15:16
  • This helped! Thanks! Although I wonder why the ``validation.yml`` constraints wouldn't work... – VMC Feb 22 '17 at 15:38
1

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.

jspizziri
  • 793
  • 1
  • 9
  • 24
0

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

Community
  • 1
  • 1
Debreczeni András
  • 1,597
  • 10
  • 18