php - 将数据传递给集合类型 symfony 中的子表单
问题描述
我使用 symfony4.1 nad 我用构建器创建了表单,例如:
->add('products', CollectionType::class, array(
'entry_type' => FrontProductsType::class,
'entry_options' => [
'label' => 'Produkt',
],
'label' => 'Dodaj produkty',
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'required' => false,
'attr' => [
'class' => 'products-select',
],
))
我的问题是如何将数据传递给集合类型内的这个实体类?
这是这个嵌入的形式:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('product', EntityType::class, array(
'placeholder' => 'Wyszukiwarka',
'mapped' => false,
'multiple' => false,
'class' => Products::class,
'data' => $options['product'],
'attr' => [
'class' => 'chosen-select order_product',
'data-placeholder' => 'Wybierz produkt',
],
'choice_attr' => function($product) {
$cena = $product->getJson()["products"]["cena"];
$stock = $product->getJson()["products"]["stock"];
$vat = $product->getJson()["products"]["vat"];
return array(
'class' => 'single_option',
'single_price' => $cena,
'stock_available' => $stock,
'single_vat' => $vat,
);
},
'choice_label' => function ($product) {
return '' . $product->getJson()["products"]["name"] . ' | Stan Magazynowy: ' . $product->getJson()["products"]["stock"] . ' | Cena netto: ' . $product->getJson()["products"]["cena"] .'';
},
'label' => 'Wybierz produkty'
))
->add('quantity', IntegerType::class, [
'label_attr' => array('class' => 'bmd-label-floating'),
'label' => 'Ilość',
'mapped' => false,
'required' => true,
'data' => $options['quantity'],
'attr' => [
'class' => 'form-control single-quantity-field',
]])
->add('cena', TextType::class, [
'label_attr' => array('class' => 'bmd-label-floating'),
'label' => 'Koszt brutto',
'mapped' => false,
'required' => true,
'data' => $options['cena'],
'attr' => [
'read_only' => true,
'class' => 'form-control single-price-field'],
]);
$builder->get('product')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) {
$form = $event->getForm();
$json = $form->getData()->getJson();
$data = $form->getData();
foreach ($json as $prop) {
foreach ($prop as $key => $value) {
$$key = $value;
}
}
$cena = number_format($cena, 2);
$form->getParent()->add('cena',TextType::class, [
'label_attr' => array('class' => 'bmd-label-floating'),
'label' => 'Koszt brutto',
'mapped' => false,
'required' => true,
'data' => $cena,
'attr' => [
'read_only' => true,
'class' => 'form-control single-price-field', 'value' => $cena],
])
->add('quantity', IntegerType::class, [
'label_attr' => array('class' => 'bmd-label-floating'),
'label' => 'Ilość',
'mapped' => false,
'required' => true,
'attr' => [
'class' => 'form-control single-quantity-field',
'min' => 1,
'max' => $stock,
]]);
}
);
}
public function getBlockPrefix()
{
return 'product';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Products::class,
'product' => null,
'quantity' => null,
'cena' => null
]);
}
}
当我保存时,一切看起来都很好。当我传递一些产品时,数组中出现了许多产品,但没有传递值以嵌入形式设置。知道如何从 collectiontype 传递数据来填充子表单吗?
解决方案
如果主表单(我们称之为YourFormType)没有data_class,您可以通过数组设置数据,使用字段名称进行索引。
$form = $this->createForm(YourFormType::class, ["products" => $array_of_Products]);
推荐阅读
- python - 如何使用 matplotlib 使用 .txt 文件中保存的数据绘制数据?
- image-processing - 用magick mogrify裁剪/交换部分图像
- javascript - 在 Angular 中不使用 innerHTML 将锚链接添加到纯文本
- python - 为什么 <= 在 Python 中评估字符串的一系列字符?
- docker - docker-compose up 给出错误:您是否尝试将目录挂载到文件上?
- azure - 避免在 Azure Api 终结点上重复 https 调用的策略
- vb.net - ContextMenuStrip 位置点
- python - 使用条件用同一列中的前一个字符串填充空值
- java - 完美平方 leetcode - 带记忆的递归解决方案
- python - 如何使用正则表达式在 python 中使用来自文件名的信息创建列?