An object of type Symfony2 adds an additional option

I have the following Symfony form field, this is a drop that loads from an object:

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

As you can see, I added 'empty_value' => '' and everything works fine. Now, I want to add an additional parameter at the end, to add let say new measure unit . In other words, the drop-down list should display all the contents of my entity, an empty value and another additional option called new measure unit or something that I want to call. Is it possible?

Edit: The entire form type file has the following:

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

Error: 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 Form work file:

 <?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 } } 
+7
php forms symfony entity
source share
1 answer

In your form type, override the finishView function:

 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 } 

You will get a new β€œadd new” option with the value β€œadd” to the bottom of the field.

+15
source share

All Articles