我有一個ChoiceType :: Type字段,顯示一些選擇,並且我想爲每個選項添加一個輸入以在其上添加一個價格。我這樣做是這樣的:Symfony - 添加並保留額外的字段
->add('product_price', ChoiceType::class, array(
'choices' => array(
"Product 1",
"Product 2",
),
)
是添加輸入每個選擇的JS:
var productBoxes = $("[id^=product_]");
// Listen the checkbox to display or hide the prices inputs
productBoxes.each(function (index) {
var priceField = '<label class="control-label required" for="product_price_' + index + '">Capacité</label>' +
'<input type="text" id="product_price_' + index + '" name="product[price][]" class="form-control">';
$(this).click(function() {
if ($(this).is(':checked')) {
$(this).parent().append(priceField);
}
})
})
JavaScript的作品,其追加旁邊的每個選擇字段。現在我想發送一個數組中的數據,如下所示: [「Product 1」=>「附加字段的值」]
但我不知道如何獲取額外的數據並將其保存到數據庫。
有人知道該怎麼做嗎?
編輯1 我試圖用CollectionType做,但沒有找到如何呈現每個CollectionType元素作爲複選框。有沒有辦法這樣做?
感謝您的幫助!
是的,我嘗試過,但沒有找到如何1. Render CollectionType作爲複選框。 2.在產品選擇上附加價格輸入。有沒有辦法做到這一點? – Boulboulouboule
您可以在ProductPriceType中添加複選框字段。它看起來像每個產品條目的複選框。然後保存已選中的數組項目複選框 – Victor