建立一个包含复选框的表单,该复选框用于学说集合中的每个实体 [英] Build a form having a checkbox for each entity in a doctrine collection

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

问题描述

我正在显示一个经过过滤的实体集合的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 \Me\MyBundle\Form\Type\FooEntitySelectionType,
        $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 \Me\MyBundle\Form\Type\FooEntitySelectByIdentityType,
        $collection_of_foo
    )
    ->createView()
;

,然后在接收已发布数据的控制器操作中:

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

$form = $this
    ->createForm(new \Me\MyBundle\Form\Type\FooEntitySelectByIdentityType)
;
$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天全站免登陆