The idea is to first validate if all the required fields are not blank. If all the required data is provided then validate if the values entered are correct. The usual case for groups sequence. How ever when I apply new GroupSequence(["Basic", "Strict"])
to the validation_groups
option it turns out the form to be valid even if all the fields are blank. If validation_groups
value is set to ["Basic", "Strict"]
the form is validated properly but with all constraints, and that is not what I want. What am I doing wrong?
Here is my code:
class MyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("name", null, [
"constraints" => new NotBlank(["groups" => ["Basic"]])
])
->add("phone", MyPhoneType::class, [
"constraints" => [
new NotBlank(["groups" => ["Basic"]]),
new PhoneNumber(["groups" => ["Strict"])
]
])
->add("email", EmailType::class, [
"constraints" => [
new NotBlank(["groups" => ["Basic"]]),
new Email(["groups" => ["Strict"]]),
],
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
"validation_groups" => new GroupSequence(["Basic", "Strict"])
]);
}
}
What am I doing wrong?
This is a known bug of Symfony version 2.8 and is fixed newer versions.
But I also did not see any documentation which shows that you can use a GroupSequence
when configue the validation_groups
option.
I would assume that when using Symfony 2.8 a custom validator which takes care of all validation is the best choice of handling such a situation.