嵌入表单集合。空结果

Embedding a collection of forms. Empty results

我正在关注这里的问答:

我的文件是:

权限集合类型:

class PermissionsCollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('permissions', CollectionType::class, [
                'entry_type' => PermissionsContentsType::class,
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => null
        ));
    }
}

权限内容类型:

class PermissionsContentsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Name',
                'attr' => [
                    'placeholder' => 'Name'
                ]
            ])
            ->add('view', CheckboxType::class, [
                'mapped' => false,
                'required' => false,
                'label' => false
            ])
            ->add('new', CheckboxType::class, [
                'mapped' => false,
                'required' => false,
                'label' => false
            ])
            ->add('edit', CheckboxType::class, [
                'mapped' => false,
                'required' => false,
                'label' => false
            ])
            ->add('delete', CheckboxType::class, [
                'mapped' => false,
                'required' => false,
                'label' => false
            ])
            ->add('accept', CheckboxType::class, [
                'mapped' => false,
                'required' => false,
                'label' => false
            ])
        ;
    }

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

控制器:

public function permissionsAction(Request $request, EntityManagerInterface $em, $role, $type, UserInterface $user)
{
    $categoriesRepository = $em->getRepository('App:Categories');
    $list = $categoriesRepository->findAll();

    $form = $this->createForm(PermissionsCollectionType::class, $list);

    $form->handleRequest($request);

    return $this->render('Acl\permissionForm.html.twig', [
        'list' => $list,
        'form' => $form->createView(),
    ]);
}

树枝:

{% extends 'base.html.twig' %}

{% block body %}

{{ form(form) }}

{% endblock %}

不幸的是,表单只显示一个词 'Permissions',仅此而已,就像列表不存在一样。可能是什么问题或如何解决?

您的 PermissionCollectionType 将查看您的 $list 数组以找到不存在的 permissions 键。要解决此问题,请尝试在将密钥提供给表单之前设置密钥(就像在您的链接问题+答案中所做的那样):

//...
$list = array('permissions' => $list);
$form = $this->createForm(PermissionsCollectionType::class, $list);
//...