簡體   English   中英

Symfony 表單 CollectionType 刪除空值

[英]Symfony form CollectionType remove empty value

我正在使用 Symfony 3.3 並且我有一個帶有 CollectionType 的表單,例如:

$builder->add('links', CollectionType::class, array(
    'label' => false,
    'entry_type' => LinkType::class,
    'entry_options' => ['data_class' => CompanyLink::class],
    'allow_add' => true,
    'allow_delete' => true,
    'prototype' => true,
    'by_reference' => false,
));

我遵循了文檔: https : //symfony.com/doc/current/reference/forms/types/collection.html

這就是我的看法:

在此處輸入圖片說明

因此,該表單已在我的數據庫中顯示了良好的數據(每次一行),但它總是另外添加一個空字段。

如何刪除這個空字段? 因為我只想擁有這條線,所以我點擊了“Ajouter”按鈕。

謝謝!

我認為在您的渲染表單功能中,您有 setData(),

要呈現沒有數據的表單,您只需要執行以下示例:

//Your main entity
$mainEntity = new Entity();
//adding the other entity into collection variable of other entity of main entity
$mainEntity->addOtherEntity(new otherEntity());

$form = $this->createForm(
        MainEntityForm::class,
        $mainEntity
);

希望它有幫助。

這篇文章有點老了,希望不要太晚。 我找到了一個解決方案,即使你用一根樹枝移動 collectionType 字段。 為避免在 for 循環后出現此空字段,請添加

{% do form.your_field.setRendered %}

它對我有用,希望它對你也有用:)

暫無
暫無

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

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