Symfony2表单:选择一个实体或添加一个新实体 [英] Symfony2 Form : Select an entity or add a new one

查看:77
本文介绍了Symfony2表单:选择一个实体或添加一个新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个order和一个client实体.

我想知道是否可以使用实际的Symfony2表单系统创建一个订单表单,以便进行以下操作:

I am wondering if it's possible with the actual Symfony2 form system to create an order form which will allow to:

  1. 从下拉列表中选择几个客户端(collectionentity表单类型的混合)
  2. 如果不在下拉列表中,则可以即时创建新客户端(collection类型的默认方式).
  1. Select several clients from a dropdown (mix of collection and entity form type)
  2. And to create new clients on the fly (the default way for the collection type) if not in the dropdown list.

我已经看到了通过在同一页面中创建多个表单来完成此操作的某种方法,但这不是我想要实现的方法.

I've seen some way to do it by creating multiple forms in the same page, but this is not the way I would like to achieve it.

有没有更好的方法可以做到这一点?

Are there any better ways to do this?

推荐答案

我遇到了类似的问题,可能会导致您解决该问题:

I had a similar problem which may lead to your resolution:

我有一个类别和项目关系(多对一),我想选择一个现有项目或创建一个新项目.

I have a Category and Item relationship (Many-to-One) and I wanted to either select an existing item or create a new item.

在我的Form类中:

    $builder->add('item', 'entity', array(
        'label' => 'Item',
        'class' => 'ExampleItemBundle:Item',
    ));

    $builder->add('itemNew', new EmbedItemForm(), array(
        'required' => FALSE,
        'mapped' => FALSE,
        'property_path' => 'item',
    ));

    $builder->addEventListener(FormEvents::PRE_SUBMIT, function(FormEvent $event) {
        $data = $event->getData();
        $form = $event->getForm();

        if (!empty($data['itemNew']['name'])) {
            $form->remove('item');

            $form->add('itemNew', new EmbedItemForm(), array(
                'required' => TRUE,
                'mapped' => TRUE,
                'property_path' => 'item',
            ));
        }
    }); 

这篇关于Symfony2表单:选择一个实体或添加一个新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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