Symfony2表单类型实体添加额外选项 [英] Symfony2 form type entity add extra option

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

问题描述

我有以下Symfony表单字段,它是从实体加载的下拉列表:

I have the following Symfony form field, it's a drop down that loads from an entity:

->add('measureunit', 'entity', array('label' => 'Measure Unit',
            'class' => 'TeamERPBaseBundle:MeasureUnit',
            'expanded' => false, 'empty_value' => '',
            'multiple' => false, 'property' => 'abreviation'
        ))

如您所见,我已添加'empty_value'=> ’ c,一切正常。现在,我想要的是在末尾有一个额外的选项来添加一个新的新度量单位。换句话说,下拉列表应显示我实体的所有内容,空值和其他名为新度量单位的额外选项,或我想调用的任何其他内容。可以吗?

As you can see I have added 'empty_value' => '' and everything works fine. Now, what I want is to have an extra option at the end to add a let say new measure unit. In other words the dropdown should display all the content of my entity, the empty value and other extra option called new measure unit or what ever I want to call it. Is it possible?

编辑:整个表单类型文件具有以下内容:

The whole form type file has this:

<?php
namespace TeamERP\StoresBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ProductType  extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('name', 'text', array('label'=>'Product name', 'required' => true,
        'attr' => array('class' => 'form-control')))
        ->add('code', 'text', array('label'=>'Code', 'required' => false,
        'attr' => array('class' => 'form-control')))
        ->add('description', 'text', array('label'=>'Description', 'required' => false,
        'attr' => array('class' => 'form-control')))
        ->add('cost', 'money', array('label'=>'Cost', 'divisor' => 100, 'currency' => 'BWP'))
        ->add('category', new CategoryType(), array('required' => false))
        ->add('measureunit', 'entity', array('label' => 'Measure Unit',
            'class' => 'TeamERPBaseBundle:MeasureUnit',
            'expanded' => false, 'placeholder' => '',
            'multiple' => false, 'property' => 'abreviation'
        ))
        ->add('qtyToPurchase', 'number', array('label'=>'Quantity to purchase', 'required' => false,
        'attr' => array('class' => 'form-control')))
        ->add('reorderPoint', 'number', array('label'=>'Reorder point', 'required' => false,
        'attr' => array('class' => 'form-control')))
        ->add('qtyOnSalesOrder', 'number', array('label'=>'Quantity on sales order', 'required' => false,
        'attr' => array('class' => 'form-control')));
    }
public function getName()
    {
        return 'product';
    }
public function finishView(FormView $view, FormInterface $form, array $options)
    {
        $new_choice = new ChoiceView(array(), 'add', 'add new'); // <- new option
        $view->children['measureunit']->vars['choices'][] = $new_choice;//<- adding the new option 
    }
}

错误:
编译错误: TeamERP\StoresBundle\Form\Type\ProductType :: finishView()的声明必须与Symfony\Component\Form\FormTypeInterface :: finishView(Symfony\Component\Form\FormView $ view兼容,Symfony\Component\Form\FormInterface $ form,数组$ options)

Edit2 工作表文件:

<?php
namespace TeamERP\StoresBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView; 
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
class ProductType  extends AbstractType 
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('name', 'text', array('label'=>'Product name', 'required' => true,
        'attr' => array('class' => 'form-control')))
        ->add('code', 'text', array('label'=>'Code', 'required' => false,
        'attr' => array('class' => 'form-control')))
        ->add('description', 'text', array('label'=>'Description', 'required' => false,
        'attr' => array('class' => 'form-control')))
        ->add('cost', 'money', array('label'=>'Cost', 'divisor' => 100, 'currency' => 'BWP'))
        ->add('category', new CategoryType(), array('required' => false))
        ->add('measureunit', 'entity', array('label' => 'Measure Unit',
            'class' => 'TeamERPBaseBundle:MeasureUnit',
            'expanded' => false, 'placeholder' => '',
            'multiple' => false, 'property' => 'abreviation'
        ))
        ->add('qtyToPurchase', 'number', array('label'=>'Quantity to purchase', 'required' => false,
        'attr' => array('class' => 'form-control')))
        ->add('reorderPoint', 'number', array('label'=>'Reorder point', 'required' => false,
        'attr' => array('class' => 'form-control')))
        ->add('qtyOnSalesOrder', 'number', array('label'=>'Quantity on sales order', 'required' => false,
        'attr' => array('class' => 'form-control')));
    }
public function getName()
    {
        return 'product';
    }
public function finishView(FormView $view, FormInterface $form, array $options)
    {
        $new_choice = new ChoiceView(array(), 'add', 'add new'); // <- new option
        $view->children['measureunit']->vars['choices'][] = $new_choice;//<- adding the new option 
    }
}


推荐答案

在表单中键入函数 finishView

In your form type override the function finishView:

public function buildForm(FormbuilderInterface $builder, array $options){
    $builder->add('measureunit', EntityType::class, array(
        'label' => 'Measure Unit',
        'class' => 'TeamERPBaseBundle:MeasureUnit',
        'expanded' => false, 
        'empty_value' => '',
        'multiple' => false, 
        'property' => 'abbreviation'
    ));
}

public function finishView(FormView $view, FormInterface $form, array $options)
{
    $newChoice = new ChoiceView(array(), 'add', 'Add New'); // <- new option
    $view->children['measureunit']->vars['choices'][] = $newChoice;//<- adding the new option 
}

您将在页面底部看到一个值为 add的新选项 add new。字段。

You will get a new option 'add new' with value 'add' to the bottom of the field.

这篇关于Symfony2表单类型实体添加额外选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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