Description
Bug Report
Q | A |
---|---|
Version | 5.1.1 |
Previous Version if the bug is a regression | 5.1.1 |
Summary
Recently, I upgraded my Symfony version from 6.3 to 7.2. I've noticed that the Unique Validator stopped working when validation groups were present.
Code without groups, the validator catches the error and prevents the duplicate in the database:
<?php
namespace App\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
#[MongoDB\Document]
#[MongoDBUnique(fields: ['email'])]
class User
{
#[MongoDB\Id]
protected string $id;
#[MongoDB\Field(type: 'string')]
protected string $email;
}
Validation groups in use, the validator is not executed, causing a duplicate in the database:
<?php
namespace App\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique as MongoDBUnique;
#[MongoDB\Document]
#[MongoDBUnique(fields: ['email'], groups: ['registration'])]
class User
{
#[MongoDB\Id]
protected string $id;
#[MongoDB\Field(type: 'string')]
protected string $email;
}
Current behavior
The Unique validator is not executed when the validation groups are present.
Expected behavior
The Unique validator should work with and without validation groups.
How to reproduce
Install Symfony 7.2 with doctrine/mongodb-odm-bundle
.
Create a document with validation groups (example above).
Configure the form to use the validation group:
namespace App\Form\Type;
use App\Document\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('email', EmailType::class)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
'validation_groups' => ['registration'],
]);
}
}
Handle form submission in the controller:
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$dm->persist($user);
$dm->flush();
}
Try to submit the form twice using the same email. It will create two documents with the same email, but it should display a validation error on the second attempt.