[英]Symfony 5.4 - EntityType multiple form field not selecting options in data
將 Symfony 從 5.2 更新到 5.4 后,我開始遇到這個問題。
我有一個表單字段,它是過濾器 UI 的一部分,用戶 select 在此表單中有多個狀態以過濾數據列表。 表單在提交后重新顯示,以便用戶看到已選擇(和應用)的過濾器。
在表單 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,
])
數據應該通過獲取類型的實體數組來設置,這些實體由在選項中傳遞的此方法查詢 - 來自表單提交的 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
如果我取消注釋 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
}
但是,表單字段不會 select 返回的實體,它只是顯示表單,就好像沒有選擇任何內容一樣。 我不認為這是因為它返回了一個代理 class; 如果我從代理查詢實體名稱,它會很好地重新調整。 對這里可能發生的事情有什么想法嗎? 同樣的代碼在 5.2 及之前的版本中有效。
更新 1.7.22
仍在調查該問題 - EntityType 選擇表單的數據屬性設置有多個值(代表多表單字段上的選定選項),但在呈現時,沒有選擇任何內容。
我最終跟蹤到供應商/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 與(false)相同?
choice.label: choice.label|trans({}, choice_translation_domain) }} {%- endif -%} {% endfor %} {%- endblock choice_widget_options -%}
變量值是用來檢查是否選擇了該選項的值。 我在上面的代碼中添加了調試和轉儲,它確認值只是一個空數組。 當我恢復到 5.2 時,它顯示了一個包含我期望的值的數組:
<option debug array(2) { [0]=> string(1) "2" [1]=> string(1) "3" }...
我很難確定將值傳遞給此 twig function 的位置。有任何指示嗎?
您必須將 '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.