symfony表单集合数据转换器 - 检测到一个循环

时间:2018-01-13 16:52:13

标签: forms symfony

我已经使用数据转换器创建了Symfony集合表单,但在提交表单期间出现此错误:"检测到一个循环。如果尚未设置表单数据,则PRE_SET_DATA事件的侦听器不能调用getData()。您应该在FormEvent对象上调用getData()。"

class BankTransferPackageType extends AbstractType
{
    /** {@inheritdoc} */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('transfers', CollectionType::class, [
            'entry_type' => BankTransferType::class,
            'allow_add' => true,
            'prototype' => false,
        ]);

        $builder->add('save', SubmitType::class);
    }

    /** {@inheritdoc} */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => BankTransferPackage::class
            ]
        );
    }

    /** {@inheritdoc} */
    public function getBlockPrefix()
    {
        return '';
    }
}

class BankTransferType extends AbstractType
{
    /** {@inheritdoc} */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('order', OrderIdHiddenType::class);
    }

    /** {@inheritdoc} */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => BankTransfer::class
            ]
        );
    }

    /** {@inheritdoc} */
    public function getBlockPrefix()
    {
        return '';
    }
}

class OrderIdHiddenType extends AbstractType
{
    /** @var OrderToIdTransformer */
    protected $orderToIdTransformer;

    /**
     * OrderIdHiddenType constructor.
     *
     * @param OrderToIdTransformer $orderToIdTransformer
     */
    public function __construct(OrderToIdTransformer $orderToIdTransformer)
    {
        $this->orderToIdTransformer = $orderToIdTransformer;
    }

    /** {@inheritdoc} */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addModelTransformer($this->orderToIdTransformer);
    }

    /** {@inheritdoc} */
    public function getParent()
    {
        return HiddenType::class;
    }
}

当我改变时:

$builder->add('order', OrderIdHiddenType::class);

为:

$builder->add('test', TextType::class);

表单提交效果很好

我需要设置:'原型' => false,因为我的OrderToIdTransformer会因为收到空数据而抛出异常。

有什么问题以及如何解决?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

我已修复它(原型为true,如果在变换器中为null,则返回null),但有没有人知道如何将OrderIdHiddenType更改为OrderIdCheckboxType,其value属性等于数据库中的order id?