EntityType上的Symfony Select2:不加载HTML中的所有选项

时间:2016-07-19 14:46:16

标签: ajax jquery-select2 symfony

我有以下表格。对于deviceparts,我想抑制Symfony将所有选项加载到HTML中,因为我已经使用Select2钩子通过Ajax加载选项,并且添加选项会增加很多膨胀(有超过4000个部分。)

我该怎么办?我尝试添加'choices' => array(),它确实提供了一个空列表,但结果是无效的表单,因为这意味着没有有效的可用选项。

<?php
public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
        ->add('device', EntityType::class, array('label' => 'Toestel', 'class' => 'AppBundle:Device', 'choice_label' => function($device) {
              return $device->getBrand()->getName().' '.$device->getName();
        }))
        ->add('parts', EntityType::class, array('label' => 'Onderdelen', 'class' => 'AppBundle:Part', 'choice_label' => 'name', 'multiple' => true))
        ->getForm();
}
?>

2 个答案:

答案 0 :(得分:0)

请改用querybuilder。这将向您展示一个很好的例子:

http://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities

我想你可以从上面的链接中找到答案......

根据您的意见编辑#2

尝试使用&#39;选择&#39;选项如下所示:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
        ->add('device', EntityType::class, array(
                'label' => 'Toestel',
                'class' => 'AppBundle:Device',
                'choices' => $device->getBrand()->getName().' '.$device->getName(),
        }))
        ->add('parts', EntityType::class, array(
                'label' => 'Onderdelen',
                'class' => 'AppBundle:Part',
                'choice_label' => 'name',
                'multiple' => true))
        ->getForm();
}

不确定这对你有用,但可能会。 变量$device需要作为表单选项传入,或者作为表示对象AppBundle:Device的变量传递给其他地方。

试试这个,看看它是否适合你!

编辑#3: 根据您的意见。通过加载AJAX,我理解你的意思。你在用什么?也许&#39; onload&#39;对于身体?你没有显示代码。

然而,也许最好的解决方案是具有空数组的ChoiceType。如果空数组不起作用,请尝试在其中加入一些内容。

尝试这些建议。我只为“设备”做过这样的事情。下拉列表,因为我不确定您需要哪一个:

空数组:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
        ->add('device', ChoiceType::class, array(
                'label' => 'Toestel',
                'choices' => array(
                            //null
                     ),
        }))
        ->add('parts', EntityType::class, array(
                'label' => 'Onderdelen',
                'class' => 'AppBundle:Part',
                'choice_label' => 'name',
                'multiple' => true))
        ->getForm();
}

包含垃圾的数组:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
        ->add('device', ChoiceType::class, array(
                'label' => 'Toestel',
                'choices' => array(
                            'Something' => true,
                     ),
        }))
        ->add('parts', EntityType::class, array(
                'label' => 'Onderdelen',
                'class' => 'AppBundle:Part',
                'choice_label' => 'name',
                'multiple' => true))
        ->getForm();
}

试试吧!

答案 1 :(得分:0)

找到各种解决方案。它并不出色,但它超过了4000个HTML选项标签。

它比我想象的要难,但是使用alsatian / form-b​​undle,它运行正常。从编码器本人那里阅读More info on alsatian/form-bundle

您可能会稍微调整一下,特别是如果您不使用MongoDB。例如。在捆绑的services.yml我必须发表评论:

- [setDocumentManager,["@doctrine.odm.mongodb.document_manager"]]

为了安装它,您必须将composer.json中的minimum-stability设置为dev - 并且为了不破坏其他所有内容,您还应将prefer-stable设置为true

以便它可以在MySQL设置上运行。否则,它也不是很擅长为Select2元素设置选项。我能够调整AbstractRoutableType.php一点,所以它也可以采用不同的请求方法,但是没有高级数据解析,因为你需要JavaScript(我不认为HTML {{1属性可以传达函数)。

所以最后,我仍然在我的Twig文件中配置data-个标签之间的所有选项。