Symfony中的嵌套形式 [英] Nested form in Symfony

查看:94
本文介绍了Symfony中的嵌套形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的嵌套实体:

I have a nested entity like this:

<?php
/**
 * @ORM\Entity(repositoryClass="App\Repository\BinderRepository")
 */
class Binder
{
/**
 * @ORM\Id
 * @ORM\GeneratedValue
 * @ORM\Column(type="integer")
 *
 * @Groups({"export"})
 *
 * @var int
 */
private $id;

/**
 * @ORM\Column(type="string", length=125)
 *
 * @Groups({"export"})
 *
 * @var string
 */
private $name;

/**
 * @ORM\ManyToMany(targetEntity="App\Entity\Binder")
 *
 * @Groups({"export"})
 * @MaxDepth(10)
 *
 * @var Collection
 */
private $children;

因此,活页夹可以包含一个subBinder,而该subBinder可以包含subsubBinder等.

So, a Binder can contain a subBinder which can contain a subsubBinder etc.

我将这些实体导出为JSON,在前端对其进行修改,然后将其重新发布为JSON.

I export these entities in JSON, modify them in front-end and re-post them in JSON.

我想制作一个能够处理这种提交的表格:

I'd like to make a form which will be able to handle this kind of submission:

[{"name":"Root 1","id":1,"level":0,"is_open":true,"children":[{"name":"Child 1","id":2,"level":1}]}]

所以,我建立了这种形式:

So, I've built this form:

class BinderType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, ['label' => 'binders.name'])
        ->add('children', CollectionType::class, array(
        'entry_type'          => BinderType::class,
        'allow_add'     => true))
        ->add('create', SubmitType::class, ['label' => 'binders.create'])
    ;
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Binder::class,
    ]);
}
}

但不幸的是,它抛出了Maximum function nesting level of '256' reached, aborting!

But unfortunately, it throws a Maximum function nesting level of '256' reached, aborting!

我知道这是因为我正在创建具有无限子项的表单,但是我只想处理我正在提交的JSON(当前分为2级(根节点和subNode)). 我可以将嵌套限制为10个级别,但是我喜欢我的表单可以工作.

I know it's because I'm creating a form with infinite subchilds, but I just want to handle the JSON I'm submitting which is currently 2 levels (root and subNode). I can limit the nesting to 10 levels but I just like my form to work.

我想念什么?

推荐答案

添加递归限制.无论如何,对于symfony项目,您将需要增加php.ini上的嵌套级别.我发现256对我的许多项目而言,这是一个相当低的级别.

Add a recursion limit. Anyway you will need to increase the nesting level on your php.ini for symfony projects I've found 256 it is a quite low level for many of my projects.

class BinderType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, ['label' => 'binders.name'])
            ;
        if ($options['recursion']>0) {
            $builder
                ->add('children', CollectionType::class, array(
                    'entry_type' => BinderType::class,
                    'allow_add' => true,
                    'entry_options' => ['recursion'=>$options['recursion']-1]
                ))
            ;
        }


    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Binder::class,
            'recursion' => 10
        ]);
    }
}

这篇关于Symfony中的嵌套形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆