我想根据我在之前的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';
}
}
我不确定它是否显而易见:'documentlist'下拉列表已经包含了来自文档实体的所有值以及我尝试通过PRE_SUBMIT EventListener实现的更新其值依赖于来自其他4个下拉菜单。 4个下拉菜单有一个“应用”按钮,所以我不需要ajax请求,但我猜是Post / PreSubmit事件? 我的问题:
如何在我的树枝文件中呈现所有内容:
{% 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 %}
答案 0 :(得分:0)
1)在$ market附近添加if语句
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
中的所有值。