为学说集合中的每个实体构建一个具有复选框的表单 [英] Build a form having a checkbox for each entity in a doctrine collection

查看:15
本文介绍了为学说集合中的每个实体构建一个具有复选框的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为过滤后的实体集合显示一个 html 表,我想在每一行中显示一个复选框,作为表单的一部分,将所选实体添加到会话变量中.

I'm displaying an html table for a filtered collection of entities and I want to display a checkbox in each row as part of a form which will add the selected entities to a session var.

我认为每个复选框都应该将实体 id 作为其值,我将从表单字段数据中获取一个 id 数组(好吧,所以该值应该是对实体的间接引用,但是对于为了简单起见).

I'm thinking that each checkbox should have the entity id as its value and I'll get an array of ids from the form field data (ok, so the value ought to be an indirect ref to the entity, but for the sake of simplicity).

我尝试使用单个实体类型字段创建表单类型,映射到实体的 id 属性并嵌入到另一个具有集合类型字段的表单类型中.

I've tried creating a form Type with a single entity type field, mapped to the id property of the entity and embedded into another form Type which has a collection type field.

class FooEntitySelectByIdentityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('foo_id', 'entity', array(
            'required'  => false,
            'class'    => 'MeMyBundle:FooEntity',
            'property' => 'id',
            'multiple' => true,
            'expanded' => true
        ));
    }

# ...

class FooEntitySelectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('identity', 'collection', array(
            'type'   => new FooEntitySelectByIdentityType,
            'options'  => array(
                'required' => false,
                'multiple' => true,
                'expanded' => true,
                'attr'     => array('class' => 'foo')
            ),
        ));
    }

# ...

在控制器中,表单是使用实体集合作为初始数据创建的

and in a controller the form is created with a collection of entities as the initial data

$form = $this
    ->createForm(
        new MeMyBundleFormTypeFooEntitySelectionType,
        $collection_of_foo
    )
    ->createView()
;

呈现表单时,身份字段只有一个标签,但没有小部件.

When the form is rendered there is a single label for the identity field, but no widgets.

是否可以以这种特殊方式使用实体和集合类型字段?如果是这样,我可能做错了什么?

Is it even possible to use entity and collection type fields in this particular way? If so, what might I be doing wrong?

推荐答案

我想这会回答你的问题.

I think this will answer your question.

忘记FooEntitySelectionType.将 property_path 字段选项添加到 FooEntitySelectByIdentityType 并将 data_class 选项设置为 null:

Forget the FooEntitySelectionType. Add a property_path field option to FooEntitySelectByIdentityType and set the data_class option to null:

class FooEntitySelectByIdentityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('foo_id', 'entity', array(
            'required'      => false,
            'class'         => 'MeMyBundle:FooEntity',
            'property'      => 'id',
            'property_path' => '[id]', # in square brackets!
            'multiple'      => true,
            'expanded'      => true
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'      => null,
            'csrf_protection' => false
        ));
    }

# ...

并在您的控制器中构建FooEntitySelectByIdentityType:

and in your controller, build the FooEntitySelectByIdentityType:

$form = $this
    ->createForm(
        new MeMyBundleFormTypeFooEntitySelectByIdentityType,
        $collection_of_foo
    )
    ->createView()
;

然后在接收 POSTed 数据的控制器操作中:

and then in the controller action which receives the POSTed data:

$form = $this
    ->createForm(new MeMyBundleFormTypeFooEntitySelectByIdentityType)
;
$form->bind($request);
if ($form->isValid()) {
    $data = $form->getData();
    $ids  = array();
    foreach ($data['foo_id'] as $entity) {
        $ids[] = $entity->getId();
    }
    $request->getSession()->set('admin/foo_list/batch', $ids);
}

最后,在你的树枝模板中:

and finally, in your twig template:

{# ... #}
{% for entity in foo_entity_collection %}
    {# ... #}

    {{ form_widget(form.foo_id[entity.id]) }}

    {# ... #}

这篇关于为学说集合中的每个实体构建一个具有复选框的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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