2015-11-05 76 views
0

我的形式是自引用的實體的基礎(類具有父類)Symfony2的:如何在窗體類獲得動作名稱

對於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'; 
    } 

} 

是它在query_builder添加if一個好主意?如何獲取操作名稱? $options數組包含操作名稱,但內部非常深。

+0

您需要得到的http://stackoverflow.com/questions/22852664/how-can-i-get-the-action-name-in-a-symfony2-controller的可能重複是來自請求 –

+0

謝謝爲答覆。我會嘗試將操作名稱從控制器發送到表單。 – Psyhos

+0

@psyhos寫你的結果作爲答案,它會更好;) – Veve

回答

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']); 
} 
+0

在我的控制器我alredy使用'行動':'$ form = $ this-> createForm(new CategoryType(),$ entity,array( 'action '=> $ this-> generateUrl('category_update',array('id'=> $ entity-> getId())), 'method'=>'PUT', )); ' – Psyhos

-1

我實現的結果是這樣的: 我用EventListener我的表單類。從cookbook 瞭解到,目前我在CategoryTypebuilForm功能看起來像這樣:

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()); 
        }, 
       )); 
      } 
     }); 
    } 

我沒有動作(但我仍然不會)。

相關問題