Project created in Symfony 4.1.6 to test possible bug with use multiple groups in constraint Valid
using the component Forms.
Considerations
- Sorry for me bad English.
- Ignore business logic (I used basic logic to simplify i want do it)
- I can resolve it with other options (Using a unique group), but do not usign constraint form
Valid
with multiple grpuos.
Before starting
- You need load initial data:
INSERT INTO `country` (`id`, `name`, `iso`, `estatus`) VALUES (1, 'Brazil', 'BR', 1), (2, 'Venezuela', 'VE', 1), (3, 'Mexico', 'MX', 2);
Sorry for dont use Migrations :/
How solumate "Bug"
To simulate "Bug" you need go to route: http://localhost:8001/new
- Select all checkbox for active validation by Item
- Select Country "Mexico" in two Items and another Country in the rest
- Submit Form.
Problem/Bug
We have next relation. Enity Cart 1 -> M with Item, and a Item have a Country.
Cart Form (CartNewType) have a CollectionType of another Form (ItemNewType)
$builder->add('items', CollectionType::class, [
'required' => false,
'entry_type' => ItemNewType::class,
'by_reference' => false,
'entry_options' => [
'constraints' => [new Assert\Valid(
['groups' =>
[
'InformationExtra',
'CountryValid',
'Information',
]
]
)],
]
])
Entity Country have the next constraint in attribute $status:
@Assert\Choice({1}, groups={"CountryValid"})
If Country is Status 2 ("Mexico") launch a Form Violation
And to resolve groups
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Item::class,
'validation_groups' => function (FormInterface $form) {
$useItem = $form->get('useItem')->getData();
$groups = [];
if (true == $useItem) {
$groups[] = 'InformationExtra';
$groups[] = 'CountryValid';
$groups[] = 'Information';
}
return $groups;
}
]);
}
Case 1
If in form CartNewType > items > Constraint Valid > groups
i use group "InformationExtra" (['Information','InformationExtra','CountryValid']
) and set like INDEX 0 "InformationExtra" (['InformationExtra','Information','CountryValid']
) in ItemNewType > function configureOptions > validation_groups
dont working others groups ("Information" and "CountryValid") but if set "InformationExtra" like INDEX 1+ work "InformationExtra" and the next Group set...
Maybe the problem is produced per using a group to Attribute not Mapped ("informationExtra") in Constraint Valid.
Case 2
If in form CartNewType > items > Constraint Valid > groups
i delete group "InformationExtra" (['Information','CountryValid']
)
only work the first group set in ItemNewType > function configureOptions > validation_groups
to a mapped attribute.
Work 'InformationExtra' and 'CountryValid'... But only valid the FIRST Item in Collection (Dont trhow Form Violation if another item Select "Mexico")... to:
$groups['InformationExtra', 'CountryValid', 'Information'];
or $groups['CountryValid', 'Information', 'InformationExtra'];
Work 'InformationExtra' and 'Information'... to:
$groups['InformationExtra', 'Information', 'CountryValid'];
or $groups['Information', 'CountryValid', 'InformationExtra'];
How can use multiple groups in Form Contraint Valid and use the OptionsResolver to decide what groups use finaly?