Symfony2:如何在表单类中获取操作名称

时间:2015-11-05 14:15:44

标签: php forms symfony

我的表单基于自引用实体(类别具有父类别)

对于$csv = Get-Item "path to the source csv file\*.csv" $file = $csv.FullName Move-Item $file "path to the destination folder\newfilename.csv" -Force 字段,我希望根据操作名称使用不同的parent函数。当操作为query_builder时,update字段(下拉列表)包含所有类别,但已编辑的类别除外。这很好用。但是,当操作为parent时,父表单字段仅包含Create new值(在我的主题类别中)。 这是类别表格类 null课程:

CategoryType

namespace Dimas\CatalogBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Doctrine\ORM\EntityRepository; class CategoryType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('name') ->add('parent', 'entity', array( 'label' => 'Parent Category ', 'empty_value' => '- Main level -', 'class' => 'DimasCatalogBundle:Category', 'property' => 'getTreeName', 'required' => false, 'query_builder' => function(EntityRepository $er) use ($options) { $er->createQueryBuilder('u'); //need only if action is 'update' $er->where('u.id <> :selfId'); $er->setParameter(':selfId', $options['data']->getId()); // end if return $er; }, )); } /** * @param OptionsResolverInterface $resolver */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults(array( 'data_class' => 'Dimas\CatalogBundle\Entity\Category' )); } /** * @return string */ public function getName() { return 'dimas_catalogbundle_category'; } } 中添加if是个好主意吗?如何获取动作名称? query_builder数组包含操作名称,但它内部非常深。

2 个答案:

答案 0 :(得分:0)

从控制器操作中将选项传递到构建器中,该操作可以充当条件:

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    if ($options['action'] === 'update') {
        $callable = function (EntityRepository $er)...
    } elseif ($option['action'] === 'Create new') {
        $callable = function (EntityRepository $er) ...
    }

    ...
        'required' => false,
        'query_builder' => $callable,
}

public function setDefaultOptions(OptionsResolverInterface $resolver) {
    $resolver
        ->setDefaults(array(
            'data_class' => 'Dimas\CatalogBundle\Entity\Category'
        ))
        ->setRequired(['action'])
    ;
}

// Your controller

public function createNewAction(Request $request)
{
    $form = $this->createForm(new CategoryType(), $category, ['action' => 'Create new']);
}

public function updateAction(Request $request)
{
    $form = $this->createForm(new CategoryType(), $category, ['action' => 'update']);
}

答案 1 :(得分:-1)

我以这种方式取得成果: 我将EventListener用于表单类。从cookbook获得了解 现在builForm类中的CategoryType函数看起来像这样:

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder->add('name');

    $builder->addEventListener(FormEvents::PRE_SET_DATA, 
    function (FormEvent $event) use ($options) {
        $category = $event->getData();
        $form = $event->getForm();

        // check if the Category object is "new"
        // If no data is passed to the form, the data is "null".
        // This should be considered a new "Category"
        if (!$category || null === $category->getId()) {
            $form->add('parent', 'entity', array(
                'label' => 'Parent Category ',
                'empty_value' => '- Main level -',
                'class' => 'DimasCatalogBundle:Category', 'property' => 'getTreeName',
                'required' => false,
                'query_builder' => function(EntityRepository $er) use ($options) {
                        return $er->createQueryBuilder('u');
               },
            ));
        } else {
                $form->add('parent', 'entity', array(
                    'label' => 'Parent Category ',
                    'empty_value' => '- Main level -',
                    'class' => 'DimasCatalogBundle:Category', 'property' => 'getTreeName',
                    'required' => false,
                    'query_builder' => function(EntityRepository $er) use ($options) {
                        return $er->createQueryBuilder('u')
                            ->where('u.id <> :selfId')
                            ->setParameter(':selfId', $options['data']->getId());
                    },
                ));
            }
        });
    }

我没有采取行动(但我仍然没有)。