简体   繁体   English

Symfony 5.4 - EntityType 多表单字段未选择数据中的选项

[英]Symfony 5.4 - EntityType multiple form field not selecting options in data

I started running into this issue after updating Symfony from 5.2 to 5.4.将 Symfony 从 5.2 更新到 5.4 后,我开始遇到这个问题。

I have a form field that is part of a filter UI where users select multiple statuses in this form to filter a list of data.我有一个表单字段,它是过滤器 UI 的一部分,用户 select 在此表单中有多个状态以过滤数据列表。 The form is re-displayed after submission so the user sees what filters have been selected (and applied).表单在提交后重新显示,以便用户看到已选择(和应用)的过滤器。

In the form class, the field is an EntityType with the multiple property set:在表单 class 中,该字段是具有多个属性集的 EntityType:

        ->add('status', EntityType::class, [
            'class' => TicketStatus::class,
            'choice_label' => 'name',
            'choice_value' => 'id',
            'data' => $this->getEntityDataMultiple($options, 'status', TicketStatus::class),
            'query_builder' => function (TicketStatusRepository $repository) {
                return $repository->createQueryBuilder(TicketStatusRepository::ALIAS)
                    ->orderBy(TicketStatusRepository::ALIAS.'.name', 'ASC');
            },
            'multiple' => true,
            'required' => false,
        ])

The data should be set by getting an array of entities of the type, which are queried for by this method that is passed in the options - an array of TicketStatus id's from the form submission:数据应该通过获取类型的实体数组来设置,这些实体由在选项中传递的此方法查询 - 来自表单提交的 TicketStatus id 数组:

protected function getEntityDataMultiple(array $options, string $fieldName, string $entityName): array
{
    $entityManager = $this->managerRegistry->getManagerForClass($entityName);

    $entities = [];

    if (($data = $options['data']) && is_array($data) && isset($data[$fieldName])) {
        $entityIds = $data[$fieldName];
        if (is_array($entityIds) && count($entityIds)) {
            foreach ($entityIds as $entityId) {
                $entities[$entityId] = $entityManager->find($entityName, $entityId);
            }
            //dump($entities);
        }
    }

    return $entities;
}
enter code here

If I uncomment the dump of entities in that function, it outputs an array of TicketStatuses:如果我取消注释 function 中的实体转储,它会输出一组 TicketStatuses:

^ array:1 [▼
  5 => Proxies\__CG__\App\Model\Entity\TicketStatus {#10004 ▼
    +__isInitialized__: false
    -id: 5
    -name: null
    -order: "1"
    -isActive: "1"
    -createdAt: null
    -updatedAt: null
     …2
  }

However the form field does not select the returned entit(ies), it just displays the form as if nothing had been selected.但是,表单字段不会 select 返回的实体,它只是显示表单,就好像没有选择任何内容一样。 I don't think it's because it returns a proxy class;我不认为这是因为它返回了一个代理 class; if I query for the entity name from the proxy it retuns fine.如果我从代理查询实体名称,它会很好地重新调整。 Any thoughts on what could be happening here?对这里可能发生的事情有什么想法吗? This same code worked in 5.2 and prior.同样的代码在 5.2 及之前的版本中有效。

UPDATES 1.7.22更新 1.7.22

Still investigating the issue - an EntityType choice form has it's data property set with multiple values (representing selected options on a multi-form field) but when rendered, nothing is selected.仍在调查该问题 - EntityType 选择表单的数据属性设置有多个值(代表多表单字段上的选定选项),但在呈现时,没有选择任何内容。

I ended up tracing into vendor/symfony/twig-bridge/Resources/views/Form/form_div_layout.html.twig, which is where the choice options are rendered and the select is set (or not).我最终跟踪到供应商/symfony/twig-bridge/Resources/views/Form/form_div_layout.html.twig,这是呈现选择选项和设置(或不设置)select 的地方。

{%- block choice_widget_options -%}

    {% for group_label, choice in options %}
    {%- if choice is iterable -%}
        <optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
            {% set options = choice %}
            {{- block('choice_widget_options') -}}
        </optgroup>
    {%- else -%}
{{ choice_translation_domain is same as(false)? {{ choice_translation_domain 与(false)相同? choice.label: choice.label|trans({}, choice_translation_domain) }} {%- endif -%} {% endfor %} {%- endblock choice_widget_options -%} choice.label: choice.label|trans({}, choice_translation_domain) }} {%- endif -%} {% endfor %} {%- endblock choice_widget_options -%}

The variable value is what is checked to see if this choice is selected.变量值是用来检查是否选择了该选项的值。 I added the debug and dump in the code above, and it confirms that value is just an empty array.我在上面的代码中添加了调试和转储,它确认值只是一个空数组。 When I revert back to 5.2, it shows an array with the values that I expect:当我恢复到 5.2 时,它显示了一个包含我期望的值的数组:

 <option debug array(2) { [0]=> string(1) "2" [1]=> string(1) "3" }...

I'm having a hard time determining where value is passed to this twig function. Any pointers?我很难确定将值传递给此 twig function 的位置。有任何指示吗?

You must add 'mapped' => true to your EntityType.php:您必须将 'mapped' => true 添加到您的 EntityType.php:

->add('status', EntityType::class, [
    'class' => TicketStatus::class,
    'choice_label' => 'name',
    'choice_value' => 'id',
    'data' => $this->getEntityDataMultiple($options, 'status', TicketStatus::class),
    'query_builder' => function (TicketStatusRepository $repository) {
        return $repository->createQueryBuilder(TicketStatusRepository::ALIAS)
            ->orderBy(TicketStatusRepository::ALIAS.'.name', 'ASC');
    },
    'multiple' => true,
    'required' => false,
    'mapped' => true
])

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

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