Symfony 4 – uniqueEntity constraint validation for collection field

there was a problem for a long time with Symfony, preventing us from using a validation constraint for a collection.

For example, with a collectionType, if we add two products at once with the same title, then there will be a duplicate error.

This topic also talks about it: https://github.com/symfony/symfony/issues/17884

It looks like there has been something new this year, but I’m not sure if it really matches what I’m looking for : https://symfony.com/blog/new-in-symfony-4-3-unique-constraint

Clearly, the goal is that when I validate a collection containing several objects having the same title, or the same email address or others, there is a constraint which checks this and warns us so that we can make the changes.

So I tried to set up what was shown in the doc ‘, but I do not succeed, I do not understand how to adapt it to my case.

I would like to add a validation constraint on my CollectionType so that it verifies that each email entered in the sub-collection is unique. And I would like to do it directly from my FormType.

Like in this image, when I try to submit a form with two objects with the same email address :

enter image description here

Without trying the new constraint-unique, I’ve this :

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicata du
champ ‘[email protected]’ pour la clef ‘UNIQ_8D93D649E7927C74’

I tried something like this but it doesn’t work :

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $entreprise = $options['entreprise'];

        $builder
        ->add('utilisateurs', CollectionType::class, [
            'entry_type' => RegistrationType::class,
            'entry_options' => [
                'label' => false,
                'entreprise' => $entreprise,
            ],
            'allow_add' => true,
            'allow_delete' => true,
            'delete_empty' => true,
            'by_reference' => true,
            'prototype' => true,
            'label' => false,
            'attr' => [
                'class' => 'my-selector',
                'label' => false,
            ],
            'by_reference' => false,
        ])
        ;

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => RegistrationCollection::class,
            'entreprise' => Entreprise::class,
            'constraints' => [
                new Collection([
                    new UniqueEntity([
                        "fields" => "email",
                        "message" => "Adresse email répétée"
                    ])
                ])
            ]
        ]);
    }

And my second FormType :

RegistrationType.php :

$builder
            ->add('nom', TextType::class, [
                'attr' => [
                    'placeholder' => "Nom"
                ]
            ])
            ->add('prenom', TextType::class, [
                'attr' => [
                    'placeholder' => "Prénom"
                ]
            ])
            ->add('profession', TextType::class, [
                "required" => false,
                'attr' => [
                    'placeholder' => "Profession"
                ]
            ])
            ->add('email', EmailType::class, [
                'attr' => [
                    'placeholder' => "Adresse email"
                ]
                ])
    ;

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *