【问题标题】:Symfony2 form type entity add extra optionSymfony2 表单类型实体添加额外选项
【发布时间】:2023-03-16 14:15:02
【问题描述】:

我有以下 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 
    }
}

【问题讨论】:

    标签: php forms symfony entity


    【解决方案1】:

    在您的表单类型中覆盖函数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 
    }
    

    您将在字段底部获得一个新选项“添加新”,其值为“添加”。

    【讨论】:

    • 谢谢,我收到这个错误: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)
    • 我认为您缺少导入 FormViewFormInterface 组件,请尝试将这两行添加到使用列表 use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormInterface;
    • 谢谢你成功了,我只需要添加这个:use Symfony\Component\Form\Extension\Core\View\ChoiceView;
    • 注意:由于此值不是初始列表的一部分(将产生验证错误),您需要 (1) 在 JS 上“处理”它,以便服务器永远不会看到它,或者 (2) 使用数据转换器来“处理”。
    • @DarrylHein 最简单的方法是处理监听器。
    【解决方案2】:

    除了接受的答案:

    如果您选择添加的选项,您将收到验证错误(因为它不是有效实体),可以使用以下 sn-p 来克服此错误:

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

    然后您可以检查所选选项是否为 NULL,如果它 => 从其他输入字段中获取值。

    【讨论】:

    • 你能进一步解释这个评论吗? “然后您可以检查所选选项是否为 NULL,如果它 => 从附加输入字段中获取值。”谢谢!
    • 我的意思是 => 如果您选择该附加选项,您将收到 Symfony 本身的验证错误 -> 这就是为什么如果选择的值为 'add' - 我们将其替换为 NULL - 所以 symfony 不会抛出验证错误。然后你可以检查 - 如果字段的值为 NULL -> 然后做你的逻辑。
    猜你喜欢
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2013-10-07
    • 2013-07-09
    • 1970-01-01
    • 2012-09-05
    • 2013-06-07
    • 1970-01-01
    相关资源
    最近更新 更多