繁体   English   中英

Symfony 4 PRE_SET_DATA 事件侦听器 setData 未更新自定义类型字段

[英]Symfony 4 PRE_SET_DATA Event Listener setData not updating custom type fields

我有一个父表单类型,其中包括一个未映射的子表单类型。 子表单类型包含三个 ChoiceType 字段,由于特定的格式要求,这些字段是手动填充的。

字段加载正确,ChoiceType 字段中数据的保存处理没有问题。

我遇到的问题是,因为子项中的 ChoiceType 字段需要使用来自父项的数据进行填充,所以我正在尝试使用父项上的 PRE_SET_DATA 事件侦听器手动填充/设置这些字段的默认值 - 事件正在调用侦听器并使用setData更新字段,但是一旦加载表单,ChoiceType 字段仍然为空。

我已经测试了手动将 ChoiceType 字段中的data属性设置为与事件侦听器中设置的值相同的值,并且这可以正常工作,因此问题肯定出在setData调用上。

一个例子:

class ParentType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('child_field', ChildType::class, [
                'required' => false,
                'mapped'   => false,
                'label'    => false
            ])
            ->add('save', SubmitType::class, [
                'attr' => [
                    'class' => 'save',
                ],
            ])
            ->addEventSubscriber(new FormLoadedListener());
    }

    ...
}

class ChildType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('first_field', ChoiceType::class, [
                'required'  => false,
                'label'     => 'First Field',
                'choices'   => $this->buildFirstChoices(),
            ])
            ->add('second_field', ChoiceType::class, [
                'required'  => false,
                'label'     => 'Second Field',
                'choices'   => $this->buildSecondChoices(),
            ])
            ->add('third_field', ChoiceType::class, [
                'required'  => false,
                'label'     => 'Third Field',
                'choices'   => $this->buildThirdChoices(),
            ]);
    }
}

class FormLoadedListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            FormEvents::PRE_SET_DATA => 'onPreSetData'
        ];
    }

    public function onPreSetData(FormEvent $event): void
    {
        $data = $event->getData();
        $form = $event->getForm();
        
        $firstValue = $this->doStuffToGetFirstValue();
        $secondValue = $this->doStuffToGetSecondValue();
        $thirdValue = $this->doStuffToGetThirdValue();

        $childField = $form->get('child_field');

        $childField->get('first_field')->setData($firstValue);
        $childField->get('second_field')->setData($secondValue);
        $childField->get('third_field')->setData($thirdValue);
    }

我还尝试了其他方法来设置数据:

$form->get('child_field')->setData([
    'first_field' => $firstValue,
    'second_field' => $secondValue,
    'third_field' => $thirdValue
]);

$form->setData(['child_field' => [
    'first_field' => $firstValue,
    'second_field' => $secondValue,
    'third_field' => $thirdValue
]);

事实证明我使用了不正确的事件监听器。 正在设置数据,但被 null 值覆盖 - 我不得不使用FormEvents::POST_SET_DATA来解决问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM