2015-05-03 12 views
7

Ho il seguente campo modulo Symfony, è un menu a discesa che viene caricata da un soggetto:Symfony2 Tipo modulo per le entità aggiungere un'opzione extra

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

Come potete vedere ho aggiunto 'empty_value' => '' e tutto funziona bene. Ora, quello che voglio è avere un'opzione in più alla fine per aggiungere una diciamo new measure unit. In altre parole, il menu a discesa dovrebbe visualizzare tutto il contenuto della mia entità, il valore vuoto e l'altra opzione extra denominata new measure unit o qualsiasi altra cosa si voglia chiamare. È possibile?

Edit: L'intero file di tipo di modulo ha questo:

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

Errore: 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 Lavorare file di forma:

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

risposta

15

Nel vostro tipo di modulo di override la funzione 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 
} 

Si otterrà una nuova opzione 'aggiungi nuovo' con valore 'aggiungi' in fondo al campo.

+0

Grazie, ho ricevuto questo errore: 'Errore di compilazione: Dichiarazione di TeamERP \ StoresBundle \ Form \ Type \ ProductType :: finishView() deve essere compatibile con Symfony \ Component \ Form \ FormTypeInterface :: finishView (Symfony \ Component \ Form \ FormView $ view, Symfony \ Component \ Form \ FormInterface $ form, array $ opzioni) ' – wti

+2

Penso che manchi di importare i componenti' FormView' e 'FormInterface', prova ad aggiungere queste due righe alla lista use' usa Symfony \ Component \ Form \ FormView; usa Symfony \ Component \ Form \ FormInterface; ' – zizoujab

+1

Grazie, ha funzionato, ho solo dovuto aggiungere anche questo:' usa Symfony \ Component \ Form \ Extension \ Core \ View \ ChoiceView; ' – wti

Problemi correlati