簡體   English   中英

Symfony - FormType - 動態選擇

[英]Symfony - FormType - Dynamic choices

我有一個包含1個EntityType字段的表單,該字段必須包含根據未在第一個實體中映射的第二個EntityType字段的選項,如下所示:

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('...');

表格

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

因此,當我選擇PlaceType時,我希望Place select僅顯示idplacetype與PlaceType id匹配的位置。

我嘗試在javascript中,在PlaceType選擇上使用onChange事件,根據PlaceType實際值過濾Place選項,但我不知道如何在formType中獲取Place的PlaceType屬性。 我試過那種東西,但它不起作用

->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
   ))

有人知道如何獲取這些數據嗎? 或者如何通過其他方式動態過濾選項?

謝謝您的幫助 !

您可以使用jquery庫更簡單一點:

首先,我們使用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());
        },
    ))

接下來,在您的模板中:

{# ... #}

{{ 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