簡體   English   中英

Symfony 3.4表單EntityType選擇的值為空

[英]Symfony 3.4 form EntityType selected value is empty

我在Symfony 3.4 EntityType中遇到問題。

CategoryType.php

class CategoryType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('spec', CollectionType::class, [
                'entry_type' => SpecificationType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'label' => false,
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Category::class,
        ));
    }
}

SpecificationType.php

class SpecificationType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', EntityType::class, [
            'class' => Specification::class,
            'label' => false,
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Specification::class,
        ));
    }

    public function getBlockPrefix()
    {
        return 'specification';
    }
}

表單按預期方式呈現:“標題為文本”字段和2個選擇元素。 但問題是呈現出選擇元素未選擇所選值。

form.html.twig

{{ form_widget(form.title) }}
{{ form_widget(form.spec) }}

結果
在此處輸入圖片說明

預期結果
在此處輸入圖片說明

當在SpecificationType.php中將EntityType::class替換為TextField:class表單現在不呈現2個select元素總線2個文本輸入(預期的行為),並且分配的值正確:
在此處輸入圖片說明

我開始研究如何首先呈現這些選擇元素,並發現此塊{%- block choice_widget_options -%}負責呈現選擇元素。

此代碼塊內部是和平的代碼:

<option value="{{ choice.value }}"{% if choice.attr %}{% with { attr: choice.attr } %}{{ block('attributes') }}{% endwith %}{% endif %}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>

正是這樣的情況: {% if choice is selectedchoice(value) %} selected="selected"{% endif %}負責將selected屬性添加到選項。 valueselectedchoice(value)擴展是有點空,這就是為什么他的選擇沒有標記選項。

也許有人知道,如何解決這個問題?

更新

spec屬性的定義如下:

CategoryEntity.php

/**
 * @ORM\ManyToMany(targetEntity="Specification", inversedBy="categoryList")
 * @ORM\JoinTable(name="category_specification")
 */
private $spec;

在這里找到解決方案。

正如@Nickolaus寫道:

[..] You are having this problem because it is a compound form in your implementation and no simple form and symfony is unable to resolve which field created inside the subform needs to be used as source for the entity field

所以解決方案是:

像這樣重構SpecificationType.php

class SpecificationType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'class' => Specification::class,
            'label' => false,
        ));
    }

    public function getParent()
    {
        return EntityType::class;
    }

    public function getBlockPrefix()
    {
        return 'specification';
    }
}

使用getParent()方法,將所有字段配置移至configureOptions並刪除buildForm()方法。

終於..浪費了很多時間..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM