我无法在表单中呈现我的嵌套集合。当加载空表单时,paintOffers 中的 paintOffers
集合和 translations
集合都显示正确,但是因为我无法让嵌套的 translations
集合显示在 { {1}} 添加第二个“油漆报价”时不显示翻译。
更新:
我发现 data-prototype
的宏无法循环翻译,因为它们没有提供给原型:
data-prototype
虽然它应该与paintOffers表单中的相同:
"prototype" => FormView {#2813 ▼
+vars: array:24 [▶]
+parent: FormView {#2805}
+children: array:7 [▼
...
"translations" => FormView {#3130 ▼
+vars: array:30 [ …30]
+parent: FormView {#2813}
+children: [] # NO LANGUAGE PROVIDED
...
语言环境是通过 OfferedByDTO 构造函数提供的:
+children: array:7 [▼
...
"translations" => FormView {#3350 ▼
+vars: array:30 [▶]
+parent: FormView {#3330}
+children: array:1 [▼ # LANGUAGE IS PROVIDED
"en" => FormView {#3357 ▼
+vars: array:24 [ …24]
+parent: FormView {#3350}
+children: array:1 [ …1]
-rendered: false
-methodRendered: false
我不知道我应该更改什么以确保 public function __construct(OfferedBy $offeredBy = null)
{
$this->offeredByEntity = $offeredBy;
$this->translations = new ArrayCollection();
if (!$this->hasExistingOffer()) {
foreach (array_keys(Language::getWorkingLanguages()) as $workingLanguage) {
$this->translations->set(
$workingLanguage,
new OfferedByTranslationDataTransferObject(
null,
Locale::fromString($workingLanguage)
)
);
}
return;
}
...
集合中的数据也提供给 translations
?
PaintType(父表单):
prototype
OfferedByType
...
$builder->add(
'paintOffers',
CollectionType::class,
[
'entry_type' => OfferedByType::class,
'allow_delete' => true,
'allow_add' => true,
'allow_sequence' => false,
'attr' => [
'class' => 'tab-pane tab-pane-collection',
'data-role' => 'paints-offered-by',
],
]
);
...
OfferedByTranslationType(OfferedByType 内的翻译集合)
...
$builder->add(
'paintCodeStickerImage',
ImageType::class,
[
'label' => 'lbl.PaintCodeStickerImage',
'required' => false,
'image_class' => Image::class,
'help_text_message' => $helpTextMessagePaintCodeStickerImage,
'show_remove_image' => true
]
);
$builder->add(
'translations',
CollectionType::class,
[
'entry_type' => OfferedByTranslationType::class,
'error_bubbling' => false,
'constraints' => [new Valid()],
]
);
...
Form.html.twig
...
$builder
->add(
'description',
EditorType::class,
[
'label' => 'lbl.Description',
'required' => false,
'attr' => [
'class' => 'inputEditor',
'data-show' => 'type-text',
],
]
);
...
翻译不显示在 ...
{% macro printOfferedByCollection(offerField,locale,activeTranslationTab) %}
<div id="paint_paintOffers___name__">
...
<ul class="nav nav-tabs" role="tablist">
{# Translation tabs #}
{% for locale,translationField in offerField.translations %}
{% set tabName = 'tab' ~ locale|ucfirst %}
<li role="presentation"{% if activeTranslationTab == tabName %} class="active"{% endif %}>
<a href="#{{ tabName }}" aria-controls="{{ tabName }}" role="tab" data-toggle="tab">
{{ locale|upper|tolabel|ucfirst }}
</a>
</li>
{% endfor %}
</ul>
<div class="tab-content">
{% for locale,translationField in offerField.translations %}
{% set tabName = 'tab' ~ locale|ucfirst %}
<div role="tabpanel" class="tab-pane{% if activeTranslationTab == tabName %} active{% endif %}" id="{{ tabName }}">
<div class="form-group">
{{ form_label(translationField.description) }}
{{ form_widget(translationField.description) }}
{{ form_errors(translationField.description) }}
</div>
{{ form_rest(translationField) }}
</div>
{% endfor %}
</div>
...
<div class="panel panel-default" id="paint_paintOffers" data-role="paints-offered-by"
data-prototype="{{ formMacros.printOfferedByCollection(form.paintOffers.vars.prototype,locale,activeTranslationTab)|e('html_attr') }}">
<ul class="list-group js-collection">
{% for paintOfferForm in form.paintOffers %}
<li class="list-group-item">
{{ formMacros.printOfferedByCollection(paintOfferForm,locale,activeTranslationTab) }}
</li>
{% endfor %}
</ul>
...
</div>
...
中,只返回周围的 HTML:
data-prototype