Created
November 19, 2015 13:10
-
-
Save xphere/cb2005111e75f4ba013e to your computer and use it in GitHub Desktop.
Am I using GroupSequence correctly? Constraints aren't called when validating this form.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class MyFormType extends AbstractType | |
| { | |
| public funcion buildForm(FormBuilderInterface $builder, array $options) | |
| { | |
| $builder | |
| ->add('username', 'text', [ | |
| 'required' => true, | |
| 'validation_groups' => new GroupSequence(['Default', 'Strict']), | |
| 'constraints' => [ | |
| new NotBlank(), | |
| new Length(['min' => 3]), | |
| new Regex(['pattern' => '/^[a-zA-Z][a-zA-Z0-9_-]+$/']), | |
| new NotForbiddenUsername([ | |
| 'groups' => ['Strict'], | |
| ]), | |
| new NotExistUsername([ | |
| 'groups' => ['Strict'], | |
| ]), | |
| ], | |
| ]); | |
| } | |
| } |
On 2.3, btw.
A workaround for the time being is to set validation_groups to array(new GroupSequence(...)).
Author
Thanks for the workaround, @webmozart! Will open a PR then.
Author
Are you sure the workaround is correct? I tried it, but to no avail.
In FormValidator::validate#L67 there's a in_array($group, $constraint->groups) check which will always fail, since $group is GroupSequence and $contraint->groups will be [Default] or [Strict]…
Author
Ping @webmozart ?
I ran into this issue today with Symfony 2.7.6. Glad that the workaround does work for me. Was the issue already solved?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah! That's a bug. The GroupSequence is cast to an array, when it should simply be wrapped. Can you change the return statement to:
and open a PR? Please do also create a test if you have time.