简体   繁体   English

Symfony - FormType - 动态选择

[英]Symfony - FormType - Dynamic choices

I've a Form with 1 EntityType field that must contain choices depending of a second EntityType field that is not mapped in the first entity, like this : 我有一个包含1个EntityType字段的表单,该字段必须包含根据未在第一个实体中映射的第二个EntityType字段的选项,如下所示:

ServicePlaceType.php : ServicePlaceType.php:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('placetype', EntityType::class, array(
            "class" => "AppBundle:PlaceType",
            "choice_label" => "place",
            "mapped" => false
        ))
        ->add('idplace', EntityType::class, array(
            "class" => "AppBundle:Place",
            "choice_label" => "place"
        ))
        ->add('...');

The tables 表格

+---------+--------------+---------------+-----------+
| Service | ServicePlace |     Place     | PlaceType |
+---------+--------------+---------------+-----------+
|         | id           |               |           |
+---------+--------------+---------------+-----------+
|         | idplace >    | < id          |           |
+---------+--------------+---------------+-----------+
| id >    | < idservice  | idPlaceType > | < id      |
+---------+--------------+---------------+-----------+
| service |              | place         | placetype |
+---------+--------------+---------------+-----------+

So, when i select a PlaceType i want that the Place select shows only the places where idplacetype match the PlaceType id. 因此,当我选择PlaceType时,我希望Place select仅显示idplacetype与PlaceType id匹配的位置。

I tried in javascript, with an onChange event on the PlaceType select, that filter the Place options according to the PlaceType actual value, but i don't know how to fetch the PlaceType property of the Place in the formType. 我尝试在javascript中,在PlaceType选择上使用onChange事件,根据PlaceType实际值过滤Place选项,但我不知道如何在formType中获取Place的PlaceType属性。 I tried that kind of things, but it doesn't work 我试过那种东西,但它不起作用

->add('idplace', EntityType::class, array(
            "class" => "AppBundle:Place",
            "choice_label" => "place",
            "attr" => array("placeType" => $this->getPlaceType()), // nor like that
  ))

  ->add('idplace', EntityType::class, array(
            "class" => "AppBundle:Place",
            "choice_label" => "place",
            "attr" => array("placeType" => function ($place) {
                return $place->getPlaceType();
            }), // neither like that
   ))

Does someone know how to fetch these datas ? 有人知道如何获取这些数据吗? Or how to dynamically filter options by another way ? 或者如何通过其他方式动态过滤选项?

Thanks for the help ! 谢谢您的帮助 !

You can do it with jquery library a little more simpler: 您可以使用jquery库更简单一点:

First, we change the builder a bit for render the place type id into <option data-type="..."> using the choice_attr option: 首先,我们使用choice_attr选项更改构建器以将地点类型id渲染到<option data-type="...">

$builder
    ->add('placetype', EntityType::class, array(
        "class" => "AppBundle:PlaceType",
        "mapped" => false
    ))
    ->add('idplace', EntityType::class, array(
        "class" => "AppBundle:Place",
        'choice_attr' => function ($place) {
            // output: <option data-type="...">...</option>
            return array('data-type' => $place->getPlaceType()->getId());
        },
    ))

Next, in you template: 接下来,在您的模板中:

{# ... #}

{{ form(form) }}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
    // when the 1st <select> was changed, then update the 2nd 
    // from current value and data-type option attribute.
    $(document).on('change', '#form_placetype', function () {
        var $idplace = $('#form_idplace'),
            // current value
            placetype = $(this).val(),
            // select available options from current value
            $available = $idplace.find('option[data-type="' + placetype + '"]');

        // deselect when the 1st <select> has changed.
        $idplace.val('');
        // hide no available options from current value
        $idplace.find('option').not($available).hide();
        // show available options from current value
        $available.show();
    });

    // Update 2nd <select> on page load.
    $('#form_placetype').trigger('change');
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM