Symfony เปลี่ยนค่าแบบเลื่อนลงด้วยตัวสร้างแบบสอบถามหลังจากส่งแบบฟอร์ม

ฉันต้องการกรองรายการแบบเลื่อนลงโดยขึ้นอยู่กับตัวเลือกที่ฉันทำในรายการแบบเลื่อนลง 4 รายการก่อนหน้า ประเภทแบบฟอร์มของฉันดูเหมือนว่า:

class DocumentDeactivationType extends AbstractType {

  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('status', 'entity', array(
      'class' => 'DocumentBundle:Status', 'property' => 'name',
      'choice_label' => 'translationkey', 'label' => 'Status',
      'expanded' => false, 'multiple' => true, 'required' => false,
      'translation_domain' => 'Documents',
      'choice_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::PRE_SUBMIT, function (FormEvent $event) use ($builder)
      {
        $form = $event->getForm();
        $data = $event->getData();
        $markets = $data['markets'];
        $status = $data['status'];
        $type = $data['type'];
        $airlines = $data['airlines'];
    $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, $status, $type, $airlines){
        return $er->createQueryBuilder('e')
        ->where('e.markets IN (:markets)')
        ->andWhere('e.status IN (:status)')
        ->andWhere('e.airlines IN (:airlines)')
        ->andWhere('e.products IN (:products)')
        ->setParameter('markets', $markets)
        ->setParameter('status', $status)
        ->setParameter('airlines', $airlines)
        ->setParameter('type', $type);
      },
      ));
  });
}
    public function getName()
    {
        return 'document_deactivation';

    }
  }

ฉันไม่แน่ใจว่าชัดเจนหรือไม่: เมนูแบบเลื่อนลง 'รายการเอกสาร' อยู่ที่นั่นแล้วพร้อมกับค่าทั้งหมดจากเอนทิตีเอกสาร และสิ่งที่ฉันพยายามที่จะบรรลุผลผ่าน PRE_SUBMIT EventListener คือการอัปเดตค่าของมันขึ้นอยู่กับตัวเลือกจากเมนูแบบเลื่อนลงอื่น ๆ อีก 4 รายการ . มีปุ่ม 'ใช้' สำหรับดรอปดาวน์ 4 รายการดังนั้นฉันไม่ควรต้องการคำขอ ajax แต่ฉันเดาว่าเป็นกิจกรรมโพสต์/ส่งล่วงหน้า ปัญหาของฉัน:

  1. เมื่อเลือกค่าบางค่าจากเมนูแบบเลื่อนลง "ตัวกรอง" เช่น หากไม่อยู่ใน 'ตลาด' ฉันได้รับข้อผิดพลาด "ดัชนีที่ไม่ได้กำหนดสำหรับตลาด" ซึ่งอาจเป็นเพราะบรรทัด $markets = $data['markets'] --> ดังนั้นสิ่งที่ฉันต้องการในที่นี้คือสามารถกรองได้เฉพาะบางส่วนเท่านั้น ทุกประเภทและไม่ถูกบังคับให้เลือกทั้งหมด
  2. การอัปเดตรายการเอกสารแบบเลื่อนลงไม่ทำงานเลย ฉันไม่คิดว่าตัวสร้างแบบสอบถามกำลังทำอะไรอยู่ แต่ฉันไม่แน่ใจว่านั่นเกี่ยวข้องกับ FormEvent ของฉันหรืออาจเป็นวิธีที่ฉันเพิ่มดรอปดาวน์ลงในแบบฟอร์ม

ทุกอย่างแสดงผลอย่างไรในไฟล์ทวิกของฉัน:

    {% block filterContent %}
  {{ form_start(form) }}
  {{ form_row(form.type) }}
  {{ form_row(form.status) }}
  {{ form_row(form.markets)}}
  {{ form_row(form.airlines)}}
    <input type="submit" class="btn-primary btn btn-xs" value="Apply Filter" />

  <br clear="all" />
{% endblock %}

{% block content %}

{{ form_label(form.documentlist) }}
{{ form_widget(form.documentlist) }}
{{ form_end(form) }} </br>

<div class="row">
  {% include 'AppBundle::HelpSubmitButton.html.twig' with { 'buttonName': 'label.submit'|trans } %}
</div>
{% endblock content %}

person sonja    schedule 22.09.2017    source แหล่งที่มา
comment
2) ฉันเชื่อว่าคุณจะต้องอัปเดตรายการผ่าน AJAX เนื่องจาก PRE_SUBMIT จะเกิดขึ้นก่อนการดำเนินการ submit ดังนั้นจะใช้ค่าทั้งหมดใน documentlist   -  person user742736    schedule 27.09.2017


คำตอบ (1)


1) เพิ่มคำสั่ง if รอบ $market

if (isset($data['markets'])) {$markets = $data['markets'];}

และ

'query_builder' => function (EntityRepository $er) use ($markets, $status, $type, $airlines) {
    $query =  $er->createQueryBuilder('e')
    ->where('e.status IN (:status)')
    ->andWhere('e.airlines IN (:airlines)')
    ->andWhere('e.products IN (:products)')
    ->setParameter('markets', $markets)
    ->setParameter('status', $status)
    ->setParameter('airlines', $airlines)
    ->setParameter('type', $type);
  };

   if (isset($data['markets'])) {
     $query->andWhere('e.markets IN (:markets)')
   }

2) ฉันเชื่อว่าคุณจะต้องอัปเดตรายการผ่าน AJAX เนื่องจาก PRE_SUBMIT จะเกิดขึ้นก่อนการดำเนินการ submit ดังนั้นจะใช้ค่าทั้งหมดใน documentlist

person user742736    schedule 26.09.2017
comment
ขอบคุณ @ user742736 ฉันพยายามตั้งค่าฟังก์ชัน ajax ให้ทำตามที่คุณพูด แต่ข้อมูลที่ฉันส่งไปยังคอนโทรลเลอร์เพื่อจัดการคำขอนั้นว่างเปล่า คุณช่วยยกตัวอย่างวิธีการทำงานได้ไหม? - person sonja; 27.09.2017