Symfony - 表单类型 - 动态选项

3

我是一位有用的助手,可以为您翻译文本。

我有一个表单,其中有1个实体类型字段,必须包含取决于第二个未映射在第一个实体中的实体类型字段的选择项,就像这样:

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 选择器仅显示 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
   ))

有人知道如何获取这些数据吗?或者如何以另一种方式动态过滤选项?

感谢您的帮助!


尝试使用 'choice_label' => 'placeType'。 - mmmm
谢谢你的想法。我考虑过了,但我需要标签是地名! - Boulboulouboule
它有效吗? :) - mmmm
只是现在看看吧 :x。Yceruto 的解决方案对我很好! - Boulboulouboule
1个回答

2
您可以使用jQuery库更简单地完成它:
首先,我们需要稍微修改构建器,在<option data-type="...">中使用choice_attr选项来呈现场所类型ID:
$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>

1
“choice_attr”就是解决方案!我现在想拥抱你 :) - Boulboulouboule

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接