在Symfony2 EntityType中添加额外选项在提交时始终无效

时间:2015-12-22 03:26:54

标签: php symfony

您好我已在Symfony的“实体类型”字段中成功添加了一个额外选项。

我有以下代码:

 class ReportFilterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder
        ->setMethod('GET')
        ->add('users', 'entity', array(
            'attr' =>
                array(
                    'class' => 'form-control',
                ),
            'expanded' => false,
            'multiple' => false,
            'class' => 'AppBundle:User',
            'query_builder' => function (EntityRepository $er) {
                return $er->createQueryBuilder('u')
                    ->orderBy('u.firstName', 'ASC');
            },
        ))
        ->add('dateFrom', 'date', array(
            'attr' =>
            array(
                'id' => 'dateFrom',
                'placeholder' => 'From',
                'class' => 'form-control',
                'data-format' => "dd/MM/yyyy",
            ),
            'widget' => 'single_text',
            'html5' => false,
        ))
        ->add('dateTo', 'date', array(
            'attr' =>
            array(
                'id' => 'dateTo',
                'placeholder' => 'To',
                'class' => 'form-control',
                'data-format' => "dd/MM/yyyy",
            ),
            'widget' => 'single_text',
            'html5' => false,
        ))
        ->add('filterSubmit', 'submit', array(
            'attr' => array('class' => 'btn btn-default'),
            'label' => 'Filter'
        ))
        ->add('pdfSubmit', 'submit', array(
            'attr' => array('class' => 'btn btn-default'),
            'label' => 'Export to PDF'
        ));
    }

    public function finishView(FormView $view, FormInterface $form, array $options)
    {
        $new_choice = new ChoiceView(new User(), 'all', 'All Employees'); // <- new option
        $view->children['users']->vars['choices'][] = $new_choice;//<- adding the new option
    }
    public function getName()
    {
        return 'report_filter';
    }
}

这里的问题是,当我提交表单并选择我添加的额外选项时,它永远不会有效。为什么会这样?我看不出问题的根源。

谢谢!

0 个答案:

没有答案