I'm using Symfony 4.4 and I have form like this:
<?php
declare(strict_types=1);
namespace App;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class MainForm extends AbstractType
{
public const GROUP_CREATE = 'create';
public const GROUP_UPDATE = 'update';
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('zone', TargetingZoneForm::class, [
'targeting_validation_groups' => [
MainForm::GROUP_CREATE,
MainForm::GROUP_UPDATE,
],
])
;
}
}
class TargetingZoneForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('is_excluded', CheckboxType::class, [
'constraints' => [
new \Symfony\Component\Validator\Constraints\Type([
'type' => 'bool',
'groups' => $options['targeting_validation_groups'],
]),
],
])
->add('list', CollectionType::class, [
'entry_type' => IntegerType::class,
'entry_options' => [
'constraints' => [
new \Symfony\Component\Validator\Constraints\Range([
'min' => 1,
'max' => 2147483647,
'groups' => $options['targeting_validation_groups'],
]),
],
],
'allow_add' => true,
'allow_delete' => true,
'error_bubbling' => false,
'constraints' => [
new \Symfony\Component\Validator\Constraints\Count([
'max' => \App\Model\Zone::ZONE_LIMITATIONS_MAX,
'maxMessage' => 'You can use up to {{ limit }} zones',
'groups' => $options['targeting_validation_groups'],
]),
new \App\Constraints\Zone([
'groups' => $options['targeting_validation_groups'],
]),
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);
$resolver->setRequired(['targeting_validation_groups']);
$resolver->setAllowedTypes('targeting_validation_groups', 'array');
}
}
My payload is like this:
{
"zone": {
"list": [
"1",
"26606653111701",
"3"
],
"is_excluded": false
}
}
I want to validate all of the elements of the collection for Range constraint, and only if it is valid - fire my custom \App\Constraints\Zone constraint after if (because inside I will have all valid IDs and send a single DB query). I failed to do so with GroupSequence.
I can't use Sequentially because I'm using Symfony 4.4.