Я хотел бы отфильтровать раскрывающийся список в зависимости от выбора, который я сделал в 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, но я думаю, что события Post/PreSubmit? Мои проблемы:
- При выборе некоторых значений из раскрывающихся списков «фильтр», но, например. опуская «рынки», я получаю сообщение об ошибке «Неопределенный индекс для рынков», вероятно, из-за строки $markets = $data['markets'] --> поэтому я хочу здесь иметь возможность фильтровать только некоторые типов и не быть вынужденным выбирать их все.
- Обновление выпадающего списка документов вообще не работает. Я даже не думаю, что построитель запросов что-то делает. Но я не уверен, связано ли это с моим 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 %}
PRE_SUBMIT
произойдет непосредственно перед действиемsubmit
, поэтому он будет использовать все значения вdocumentlist
. - person user742736   schedule 27.09.2017