У меня есть форма с дочерним типом коллекции.с помощью java-скрипта я изменяю имя и идентификатор входных данных в каждой строке обычным образом, но когда я отправляю форму и выкидываю $form->getData()
, в нем есть только один элемент, который я добавил в контроллер.здесь мой раздел контроллера:
public function add(Request $request)
{
$snap = new Snap();
$snapProduct = new SnapProduct();
$snap->addProduct($snapProduct);
$form = $this->createForm(SnapType::class, $snap);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($snap);
$em->flush();
$this->addFlash('success', 'succecc');
return $this->redirectToRoute('app_panel_snap_details', ['id' => $snap->getId()]);
}
}
return $this->render('panel/snap/new.html.twig', [
'form' => $form->createView()
]);
}
и вот мой SnapType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('registered_at', JalaliDateTimeType::class)
->add('number', TextType::class)
->add('invoice', TextType::class)
->add('account')
->add('products', CollectionType::class, [
'entry_type' => SnapProductType::class,
'entry_options' => [ 'label' => false ],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Snap::class,
]);
}
и мой SnapProductType
$builder
->add('count', TextType::class, [
'label_format'=> ' '
])
->add('in_time_price', AmountType::class, [
'label_format' => ' '
])
->add('product')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => SnapProduct::class,
]);
}
и html моей формы после рендера и добавления новыхстрока с Java-скриптом:
<div class="form-group" id="snap-products">
<div class="row product-row" id="snap_products_0">
<div class="col-md-2">
<label class="pull-right" for="snap_products_0_product">product </label>
</div>
<div class="col-md-4">
<select id="snap_products_0_product" name="snap[products][0][product]" data-toggle="product-select" class="form-control">[options]</select>
</div>
<div class="col-md-3">
<div class="input-group">
<span class="input-group-addon" data-toggle="count-plus"><i class="fa fa-plus"></i></span>
<input id="snap_products_0_count" name="snap[products][0][count]" required="required" placeholder="count" data-holder="product-count" class="form-control" type="text">
<span class="input-group-addon" data-toggle="count-minus"><i class="fa fa-minus"></i></span>
</div>
</div>
<div class="col-md-2">
<input id="snap_products_0_in_time_price" name="snap[products][0][in_time_price]" required="required" placeholder="price" data-holder="product-price" data-toggle="amount-formatted" class="form-control" value="0" type="text">
</div>
<div class="col-md-1">
<button class="btn btn-sm btn-outline btn-circle btn-danger pull-left" type="button" data-toggle="remove-product">
<i class="fa fa-trash"></i>
</button>
</div>
</div>
<div class="row product-row" id="snap_products_1">
<div class="col-md-2">
<label class="pull-right" for="snap_products_0_product">product </label>
</div>
<div class="col-md-4">
<select id="snap_products_1_product" name="snap[products][1][product]" data-toggle="product-select" class="form-control">[options]</select>
</div>
<div class="col-md-3">
<div class="input-group">
<span style="cursor: pointer" class="input-group-addon" data-toggle="count-plus"><i class="fa fa-plus"></i></span>
<input id="snap_products_1_count" name="snap[products][1][count]" required="required" placeholder="count" data-holder="product-count" class="form-control" type="text">
<span style="cursor: pointer" class="input-group-addon" data-toggle="count-minus"><i class="fa fa-minus"></i></span>
</div>
</div>
<div class="col-md-2">
<input id="snap_products_1_in_time_price" name="snap[products][1][in_time_price]" required="required" placeholder="price" data-holder="product-price" data-toggle="amount-formatted" class="form-control" value="0" type="text">
</div>
<div class="col-md-1">
<button class="btn btn-sm btn-outline btn-circle btn-danger pull-left" type="button" data-toggle="remove-product">
<i class="fa fa-trash"></i>
</button>
</div>
</div>
<div class="row product-row" id="snap_products_2">
<div class="col-md-2">
<label class="pull-right" for="snap_products_0_product">product </label>
</div>
<div class="col-md-4">
<select id="snap_products_2_product" name="snap[products][2][product]" data-toggle="product-select" class="form-control">[options]</select>
</div>
<div class="col-md-3">
<div class="input-group">
<span style="cursor: pointer" class="input-group-addon" data-toggle="count-plus"><i class="fa fa-plus"></i></span>
<input id="snap_products_2_count" name="snap[products][2][count]" required="required" placeholder="count" data-holder="product-count" class="form-control" type="text">
<span style="cursor: pointer" class="input-group-addon" data-toggle="count-minus"><i class="fa fa-minus"></i></span>
</div>
</div>
<div class="col-md-2">
<input id="snap_products_2_in_time_price" name="snap[products][2][in_time_price]" required="required" placeholder="price" data-holder="product-price" data-toggle="amount-formatted" class="form-control" value="0" type="text">
</div>
<div class="col-md-1">
<button class="btn btn-sm btn-outline btn-circle btn-danger pull-left" type="button" data-toggle="remove-product">
<i class="fa fa-trash"></i>
</button>
</div>
</div>
Редактировать div # snap_products_1 и div # snap_products_2, добавленные Java-скриптом