簡體   English   中英

Symfony2表單類型實體添加額外選項

[英]Symfony2 form type entity add extra option

我有以下Symfony表單字段,它是從實體加載的下拉列表:

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

在表單類型中覆蓋函數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”。

除了接受的答案:

如果您選擇添加的選項,您將收到驗證錯誤(因為它不是有效的實體),可以使用以下代碼段來克服此錯誤:

$builder->addEventListener(
        FormEvents::PRE_SUBMIT,
        function (FormEvent $event) {
            if ($event->getData() === 'add') {
                $event->setData(null);
            }
        }
    );

然后,您可以檢查所選選項是否為NULL,如果它=>從其他輸入字段獲取值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM