Skip to content

[Validator] GroupSequence stopping at first group, never using the second #36852

Closed
@damienalexandre

Description

@damienalexandre

Symfony version(s) affected: v3.4.40

Description

I'm using Symfony v3.4.37 and tried to upgrade to v3.4.40, but it looks like the GroupSequence validation_groups is not working as expected anymore.

How to reproduce

Here is a simple test to reproduce:

<?php

namespace Tests\AppBundle\Controller;

use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Length;

class GroupSequenceTest extends WebTestCase
{
    public function testWithoutSequence()
    {
        parent::createClient();

        $formBuilder = self::$kernel->getContainer()->get('form.factory')->createBuilder(FormType::class, [], [
            'csrf_protection' => false,
            'validation_groups' => ['First', 'Second']
        ]);

        /** @var FormInterface $form */
        $form = $formBuilder
            ->add('foobar', EmailType::class, [
                'constraints' => [
                    new Email([
                        'strict' => true,
                        'groups' => ['First'],
                    ]),
                    new Length([
                        'groups' => ['Second'],
                        'max' => 3
                    ]),
                ],
            ])
            ->getForm();

        $form->submit(['foobar' => 'test@example.org']);

        $this->assertFalse($form->isValid());
    }

    public function testWithGroupSequence()
    {
        parent::createClient();

        $formBuilder = self::$kernel->getContainer()->get('form.factory')->createBuilder(FormType::class, [], [
            'csrf_protection' => false,
            'validation_groups' => new GroupSequence(['First', 'Second'])
        ]);

        /** @var FormInterface $form */
        $form = $formBuilder
            ->add('foobar', EmailType::class, [
                'constraints' => [
                    new Email([
                        'strict' => true,
                        'groups' => ['First'],
                    ]),
                    new Length([
                        'groups' => ['Second'],
                        'max' => 3
                    ]),
                ],
            ])
            ->getForm();

        $form->submit(['foobar' => 'test@example.org']);

        $this->assertFalse($form->isValid());
    }
}

The first one use two groups, the form is not valid which is what we expect.

The second test use the sames groups in a GroupSequence, this is where I see a difference between Symfony version:

  • on Symfony v3.4.37 the test is green, the two validators are checked
  • on Symfony v3.4.40 the test is red, only the first validator is checked

Possible Solution

None.

Additional context

Tested on (yes, I know 😬):

PHP 7.0.33 (cli) (built: Dec  7 2018 16:18:01) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies
    with blackfire v1.28.4~linux-musl-x64-non_zts70, https://blackfire.io, by Blackfire

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions