2015-05-03 183 views
7

我有以下的Symfony表單域,這是一個下拉菜單,從實體載荷:Symfony2的形式的實體類型添加額外的選項

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

正如你可以看到我已經添加'empty_value' => '',一切工作正常。現在,我想要的是在最後添加一個額外的選項,例如new measure unit。換句話說,下拉列表應顯示我的實體的所有內容,空值和其他額外選項new measure unit或我想要調用的其他選項。可能嗎?

編輯:整個表單類型文件有這樣的:

<?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 
    } 
} 

錯誤: Compile Error: Declaration of TeamERP\StoresBundle\Form\Type\ProductType::finishView() must be compatible with Symfony\Component\Form\FormTypeInterface::finishView(Symfony\Component\Form\FormView $view, Symfony\Component\Form\FormInterface $form, array $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 
    } 
} 

回答

15

在表單類型覆蓋功能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 new',其值爲'add'。

+0

謝謝,我得到這個錯誤:'編譯錯誤:TeamERP \ StoresBundle \ Form \ Type \ ProductType :: finishView()的聲明必須與Symfony \ Component \ Form \ FormTypeInterface :: finishView(Symfony \ Component \ Form \ FormView $ view,Symfony \ Component \ Form \ FormInterface $ form,array $ options)' – wti

+2

我覺得你缺少導入FormView和FormInterface的組件,嘗試將這兩行添加到使用列表中use Symfony \組件\表格\ FormView的;使用Symfony \ Component \ Form \ FormInterface;' – zizoujab

+1

謝謝它的工作,我只需要添加這個:'使用Symfony \ Component \ Form \ Extension \ Core \ View \ ChoiceView;' – wti

相關問題