简体   繁体   English

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

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

I have a parent form type which includes an unmapped child form type.我有一个父表单类型,其中包括一个未映射的子表单类型。 The child form type contains three ChoiceType fields which are populated manually due to a specific formatting requirement.子表单类型包含三个 ChoiceType 字段,由于特定的格式要求,这些字段是手动填充的。

The fields load correctly and saving of the data from the ChoiceType fields is handled with no issue.字段加载正确,ChoiceType 字段中数据的保存处理没有问题。

The problem I'm having is, because the ChoiceType fields in the child need to be populated using data from the parent, I am trying to manually populate/set the default values of those fields using a PRE_SET_DATA event listener on the parent - the event listener is being called and fields are updated using setData but, once the form loads, the ChoiceType fields are still empty.我遇到的问题是,因为子项中的 ChoiceType 字段需要使用来自父项的数据进行填充,所以我正在尝试使用父项上的 PRE_SET_DATA 事件侦听器手动填充/设置这些字段的默认值 - 事件正在调用侦听器并使用setData更新字段,但是一旦加载表单,ChoiceType 字段仍然为空。

I've tested manually setting the data property within the ChoiceType fields to the same value as is being set within the event listener and this works correctly, so the issue is definitely with the setData call.我已经测试了手动将 ChoiceType 字段中的data属性设置为与事件侦听器中设置的值相同的值,并且这可以正常工作,因此问题肯定出在setData调用上。

An example:一个例子:

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);
    }

I've also tried other methods to set the data:我还尝试了其他方法来设置数据:

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

And

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

It turns out I was using the incorrect event listener.事实证明我使用了不正确的事件监听器。 The data was being set but was being overridden by null values - I instead had to use FormEvents::POST_SET_DATA which fixed the issue正在设置数据,但被 null 值覆盖 - 我不得不使用FormEvents::POST_SET_DATA来解决问题

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

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