Symfony – Unique entity in CollectionType field?

I have a formBuilder that contains a collectionType. I would like to be able to put a constraint on the email field to be sure that when the user validates, there is not the same email address several times in the form

I’ve :

RegistrationCollectionType.php

$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,
        ])
        ;

With his class :

RegistrationCollection.php

class RegistrationCollection
{


    private $utilisateurs = [];

    public function getUtilisateurs(): ?array
    {
        return $this->utilisateurs;
    }

    public function setUtilisateurs(?array $utilisateurs): self
    {
        $this->utilisateurs = $utilisateurs;

        return $this;
    }
}

And in my RegistrationType.php which is associated with my User entity, I’ve :

RegistrationType.php

->add('email', EmailType::class, [
                'attr' => [
                    'placeholder' => "Adresse email"
                ],
            ])

enter image description here

Now if I valid, I’ve :

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

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

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