Php 如何在Symfony表单中将默认选项与新选项合并

Php 如何在Symfony表单中将默认选项与新选项合并,php,symfony,symfony-forms,Php,Symfony,Symfony Forms,我有一个表单和一个子表单,我想合并定义为默认值的约束值和根表单添加的主题 我的子窗体: class DatesPeriodType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('start', DateType::class, [

我有一个表单和一个子表单,我想合并定义为默认值的约束值和根表单添加的主题

我的子窗体:

class DatesPeriodType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('start', DateType::class, [
                'constraints' => [
                    new Date(),
                ]
            ])
            ->add('end', DateType::class, [
                'constraints' => [
                    new Date(),
                ]
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver
            ->setDefault('error_bubbling', false)
            ->setDefault('constraints', [
                new Callback([$this, 'validate']),
            ])
        ;
    }

}
我使用新的约束选项将表单添加到根目录:

        $builder
            ->add('judgmentPeriod', DatesPeriodType::class, [
                'constraints' => [
                    new Valid(),
                    new Callback([
                        'callback' => [$this, 'datesAreEmpty'],
                        'groups' => ['insertionPeriod'],
                    ]),
                    new Callback([
                        'callback' => [$this, 'validDates'],
                        'groups' => ['judgmentPeriod'],
                    ]),
                ]
            ])
正如预期的那样,约束选项现在包含3个元素,并且回调约束没有合并。 我尝试了这种解决方案:但似乎没有调用回调方法


谢谢,Corentin

在您的父表单类型上尝试以下操作:

...

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setNormalizer('constraints', function (Options $options, $value) {
        // Merge the child constraints with the these, the parent constraints
        return array_merge($value, [
            new Assert\Callback(...),
            ...
        ]);
    });
}

...

在父窗体类型上尝试以下操作:

...

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setNormalizer('constraints', function (Options $options, $value) {
        // Merge the child constraints with the these, the parent constraints
        return array_merge($value, [
            new Assert\Callback(...),
            ...
        ]);
    });
}

...