Skip to content

Instantly share code, notes, and snippets.

@xphere
Created November 19, 2015 13:10
Show Gist options
  • Select an option

  • Save xphere/cb2005111e75f4ba013e to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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
Copy Markdown

On 2.3, btw.

@webmozart
Copy link
Copy Markdown

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

@xphere
Copy link
Copy Markdown
Author

xphere commented Nov 20, 2015

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

@xphere
Copy link
Copy Markdown
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
Copy Markdown
Author

xphere commented Nov 30, 2015

Ping @webmozart ?

@HayekClub
Copy link
Copy Markdown

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