嵌入表单的集合.空结果 [英] Embedding a collection of forms. Empty results

查看:78
本文介绍了嵌入表单的集合.空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里回答问题: 具有一个实体的所有行的一个表单

I'm following the question and answer here: One form with all row of one entity

我的文件是:

PermissionCollectionType:

PermissionCollectionType:

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

PermissionsContentsType:

PermissionsContentsType:

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',仅此而已,就像列表不存在一样.可能是什么问题或如何解决?

Unfortunately, the form displays only a word 'Permissions' and nothing more, like the list doesn't exist. What could be the issue or how to fix that?

推荐答案

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

your PermissionCollectionType will look into your $list array to find the permissions key, which doesn't exist. To fix, try setting the key before giving it to the form (as is done in your linked question+answer):

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

这篇关于嵌入表单的集合.空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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