2017-09-21 53 views
0

我有一個過濾器表單,用於過濾市場,類型和航空公司。提交時,我希望在選定的值之後過濾另一個下拉列表(文檔列表)。因此,我創建了一個正在工作的post_submit eventlistener(我將這些值轉儲到下面的代碼中)。但是,當試圖通過查詢生成器和我的過濾器中的數據更新文檔列表值時,它不起作用。任何想法:Symfony查詢生成器與提交後數據不會更改EventListener中的值

<?php 

namespace DocumentBundle\Form\Document; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Doctrine\ORM; 
use Doctrine\ORM\EntityRepository; 
use Symfony\Bundle\FrameworkBundle\Controller; 
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; 
use Symfony\Component\Form\FormEvent; 
use Symfony\Component\Form\FormEvents; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 
use DocumentBundle\Form\Document\DocumentFilterType; 

class DocumentDeactivationType extends DocumentFilterType { 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 

    $builder 
    ->add('type', 'choice', array('choices' => array(
     'document_types.contract' => 1, 
     'document_types.general'=>2, 
     'document_types.goodwill_policy'=>3, 
     'document_types.pricesheet'=>4, 
     'document_types.yq_update'=>5, 
     'document_types.contract_addendum'=>6), 
     'choices_as_values' => true, 'label' => 'label.types', 
     'expanded' => false, 'multiple' => true, 
     'label' => 'label.type', 'required' => false, 
     'translation_domain' => 'Documents')) 

    ->add('airlines', 'entity', array(
     'class' => 'AppBundle:Airline', 'property' => 'id', 
     'query_builder' => function (EntityRepository $er){ 

     return $er->createQueryBuilder('a') 
      ->addOrderBy('a.id', 'ASC'); 
     }, 
     'choice_value' => 'id', 
     'choice_label' => 'id', 'label' => 'label.airlines', 
     'expanded' => false, 'multiple' => true, 'required' => false, 
     'translation_domain' => 'Documents')) 

    ->add('markets', 'entity', array(
     'class' => 'AppBundle:Market', 'property' => 'id', 
     'query_builder' => function (EntityRepository $er){ 
      return $er->createQueryBuilder('m') 
      ->addOrderBy('m.id', 'ASC'); 
      }, 
     'choice_value' => 'id', 
     'choice_label' => 'id', 'label' => 'label.markets', 
     'expanded' => false, 'multiple' => true, 'required' => false, 
     'translation_domain' => 'Documents')) 

    ->add('documentlist', EntityType::class, array(
     'class' => 'DocumentBundle:Document', 
     'property' => 'name', 
     'expanded' => false, 'multiple' => true, 
     'label' => 'label.document_list', 
     'empty_value' => "Select document", 
     'required' => false, 
     'mapped' => false, 
     'translation_domain' => 'Documents')); 

    $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($builder) 
     { 
     $form = $event->getForm(); 
     $data = $event->getData(); 
     $markets = $data['markets']; 
     $type = $data['type']; 
     $airlines = $data['airlines']; 
     dump($markets, $type); 
$builder 
    ->add('documentlist', EntityType::class, array(
     'class' => 'DocumentBundle:Document', 
     'property' => 'name', 
     'expanded' => false, 'multiple' => true, 
     'label' => 'label.document_list', 
     'empty_value' => "Select document", 
     'required' => false, 
     'mapped' => false, 
     'translation_domain' => 'Documents', 
     'query_builder' => function (EntityRepository $er) use ($markets, $type, $airlines){ 
     return $er->createQueryBuilder('e') 
     ->where('e.markets = :markets') 
     ->andWhere('e.airlines IN (:airlines)') 
     ->andWhere('e.products IN (:products)') 
     ->setParameter('e.markets', $markets) 
     ->setParameter('e.airlines', $airlines) 
     ->setParameter('e.type', $type); 
     }, 
    )); 
    }); 
} 
public function getName() 
{ 
    return 'document_deactivation'; 
} 

}

回答

1

你不能在POST_SUBMIT事件改變形式(添加或刪除字段)。但你可以在PRE_SUBMIT事件中,所以你只需要改變POST_SUBMITPRE_SUBMIT

但要小心,因爲您的表單數據將不會在PRE_SUBMIT中標準化,所以您將不得不使用表單(數組)中的原始數據或手動標準化數據。

+0

對不起,我只理解你一半的答案。我知道我不能在POST_SUBMIT上更改它,所以我將其更改爲PRE_SUBMIT。但是正常化部分呢?我不明白:/現在與PRE_SUBMIT事件,我仍然從我的過濾器獲取數據,但下拉列表仍然沒有更新它的值.. – sonja

+0

對不起,再次distrub你,但你有任何進一步的建議?我仍然堅持這一點.. – sonja