Skip to content

Instantly share code, notes, and snippets.

@xphere
Created November 19, 2015 13:10
Show Gist options
  • Save xphere/cb2005111e75f4ba013e to your computer and use it in GitHub Desktop.
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.
<?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'],
]),
],
]);
}
}
@webmozart
Copy link

What happens if you set the GroupSequence in configureOptions()?

@xphere
Copy link
Author

xphere commented Nov 19, 2015

No validation happens anyway.

The validation_groups option is converted in BaseValidatorExtension::configureOptions to an array like this:

[
  'cascadeGroup' => null,
  'groups' => [
    0 => 'Default',
    1 => 'Strict',
  ],
]

@webmozart
Copy link

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:

return is_array($groups) ? $groups : array($groups);

and open a PR? Please do also create a test if you have time.

@webmozart
Copy link

On 2.3, btw.

@webmozart
Copy link

A workaround for the time being is to set validation_groups to array(new GroupSequence(...)).

@xphere
Copy link
Author

xphere commented Nov 20, 2015

Thanks for the workaround, @webmozart! Will open a PR then.

@xphere
Copy link
Author

xphere commented Nov 20, 2015

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]

@xphere
Copy link
Author

xphere commented Nov 30, 2015

Ping @webmozart ?

@HayekClub
Copy link

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