I have a ChoiceType::Type field that display some choices, and i want to add, for each choices, an input to add a price on it. I did it like this :
->add('product_price', ChoiceType::class, array(
'choices' => array(
"Product 1",
"Product 2",
),
)
The JS that add the input for each choices :
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);
}
})
})
The javascript works, it append fields next to each choices. Now I want to send my data in an array, like this : ["Product 1" => "value of the attached field"]
But I don't know how to fetch that extra data and save it to the database.
Did someone knows how to do it ?
EDIT 1 I tried to do it with CollectionType, but don't find out how to render each CollectionType element as checkbox. Is there a way to do it this way ?
Thanks for your help !