Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms Symfony2分组表单项_Forms_Symfony - Fatal编程技术网

Forms Symfony2分组表单项

Forms Symfony2分组表单项,forms,symfony,Forms,Symfony,我真的需要你帮我创建一个表单。我有实体,具有$id、$name和$items属性$items是多个关系。然后我有另外两个实体组和项。两者都有$id和$name属性组和实体与项目相关。大概是这样的: Group 1 Entity 1 |- Item 1 |- Item 1 '- Item 2 |- Item 2 '- Item 3 Group 2

我真的需要你帮我创建一个表单。我有实体,具有
$id
$name
$items
属性<代码>$items是多个关系。然后我有另外两个实体。两者都有
$id
$name
属性实体项目相关。大概是这样的:

Group 1 Entity 1 |- Item 1 |- Item 1 '- Item 2 |- Item 2 '- Item 3 Group 2 Entity 2 |- Item 3 |- Item 2 '- Item 4 |- Item 3 '- Item 4 第一组实体1 |-项目1 |-项目1 '-项目2 |-项目2 “-项目3 第2组实体2 |-项目3 |-项目2 '-项目4 |-项目3 “-项目4 使用分组项为实体创建表单的最佳方法是什么?像这样:

---------------------------------------------------- Group 1 ☑ Item 1 ☑ Item 2 ---------------------------------------------------- Group 2 ☑ Item 3 ☑ Item 4 ---------------------------------------------------- 第一组 ☑ 项目1☑ 项目2 ---------------------------------------------------- 第2组 ☑ 项目3☑ 项目4
尝试用谷歌搜索,但老实说,我不知道从哪里开始://

EntityType.php
文件中(这可以基于您的实体),您将需要一个类似以下的

$builder->add('item', 'entity' array(
    'class' => 'YourNameSpace:Item',
    'property' => 'name',
    'multiple' => true,
    'expanded' => true,
    'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
        return $er->createQueryBuilder('i')
            ->where('i.group = 1');
    }
);

显然,您必须根据特定的模式和变量名进行一些调整。这还需要组和项之间的双向关联。这也仅为组1添加选择器。您必须为第二组添加另一个类似的内容。

好的。。。所以我发现,
groupby
用于获取
中的optgroup。My
EntityType
具有:

$builder->add('item', 'entity' array(
    'class' => 'MyBundle:Item',
    'property' => 'name',
    'multiple' => true,
    'expanded' => true,
    'group_by' => 'group.name',
);
在我的模板中:

{% form_theme form 'MyBundle::form_theme.html.twig' %}
{{ form_widget(form.item) }}
{{ form_rest(form) }}
在我的表单主题中,我只是尝试复制
choice\u widget\u-collapsed
,但存在一些差异(例如:检查值是否被选中)。我不知道这是否是最好的方法,但对我来说很有效。可能是因为我只使用了
实体选择,没有其他输入字段。 我的表格主题:

{% block choice_widget_expanded %}
{% spaceless %}
    <div {{ block('widget_container_attributes') }}>
        {% set options = choices %}
        {{ block('choice_widget_expanded_options') }}
    </div>
{% endspaceless %}
{% endblock choice_widget_expanded %}

{% block choice_widget_expanded_options %}
{% spaceless %}
    {% for group_label, choice in options %}
        {% if choice is iterable %}
            <fieldset>
                <legend>{{ group_label|trans({}, translation_domain) }}</legend>
                {% set options = choice %}
                {{ block('choice_widget_expanded_options') }}
            </fieldset>
        {% else %}
            <input type="checkbox" name="{{ full_name }}[]" value="{{ choice.value }}"{% if value[choice.value] %} checked="checked"{% endif %}/> {{ choice.label|trans({}, translation_domain) }}
        {% endif %}
    {% endfor %}
{% endspaceless %}
{% endblock choice_widget_expanded_options %}
{%block choice\u widget\u expanded%}
{%spaceless%}
{%set options=choices%}
{{block('choice_widget_expanded_options')}
{%endspaceless%}
{%endblock choice_widget_expanded%}
{%block choice_widget_expanded_options%}
{%spaceless%}
{%用于组_标签,选项%}
{%如果选择是可编辑的%}
{{group_label}trans({},translation_domain)}
{%set options=choice%}
{{block('choice_widget_expanded_options')}
{%else%}
{{choice.label}trans({},translation_domain)}
{%endif%}
{%endfor%}
{%endspaceless%}
{%endblock choice_widget_expanded_options%}

欢迎任何改进。

因此,首先在我的
EntityType
中,我必须获取所有组,然后为每个组调用
$builder->add()
?然后是另一个问题-如何在细枝中显示这样的组,因为,如果我理解正确,我只有表单中的项?好的。我尝试了
$builder->add('item',entity',array())
为每个组更改组ID,但在表单中我只得到最后一个组项目。我猜是因为同一个名字
'item'
。尝试了
$er->createQueryBuilder('i')->where('i.group=1')->或where('i.group=2')
,但我只得到了这些组的所有项。无法确定如何在窗体视图中对项目进行分组并输出组的名称。按组分隔项目不是重点吗?不确定这不是您想要的。重点是在一个实体表单中显示所有项目,但这些项目应该分组。类似于select标记中的
。我发现可以使用
groupby
,但无法实现我需要的:(