-
-
Save xphere/cb2005111e75f4ba013e to your computer and use it in GitHub Desktop.
<?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'], | |
]), | |
], | |
]); | |
} | |
} |
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',
],
]
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.
On 2.3, btw.
A workaround for the time being is to set validation_groups
to array(new GroupSequence(...))
.
Thanks for the workaround, @webmozart! Will open a PR then.
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]
…
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?
What happens if you set the GroupSequence in
configureOptions()
?