Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique validator does not work when validation groups are used #874

Open
blacksaildivision opened this issue Dec 23, 2024 · 2 comments
Open
Labels

Comments

@blacksaildivision
Copy link

blacksaildivision commented Dec 23, 2024

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.

@malarzm
Copy link
Member

malarzm commented Dec 30, 2024

Oh boy this is going to be fun to fix. symfony/doctrine-bridge@29f1511#diff-1a16e2739e51eab000116d0542bd0226cea59a6d64711740ed7ce14769f95d1b added a new identifierFieldNames argument to the constructor and it's added in the middle effectively breaking constraint defined in this bundle.

@malarzm malarzm added the bug label Dec 30, 2024
@malarzm
Copy link
Member

malarzm commented Dec 30, 2024

This is the PR introducing said feature symfony/symfony#38662. Not gonna lie, changing arguments order on Symfony's end would be the easiest way but it may be way too late for that as the feature was shipped in 7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants