douyong4842 2019-05-16 10:27
浏览 31

如何在Symfony中使用自定义类型正确使用FormBuilderInterface

I have 3 related Database tables (inventory, sale, sale_details) i would like to be able to make a form that can handle creating a Sale.

The problem is with my custom field "InventoryType" which should map correctly to an entity in the Database.

NOTE: Inventory are stored uniquely on their serial number.

I have created 3 custom field types, "InventoryType, SaleDetailsType, SaleType",

The InventoryType holds data equal to a Inventory table and (Inventory.php Entity)

The SaleDetailsType holds one InventoryType,

And the SaleType holds a collection of SaleDetailsTypes, account information and more.

I've tried with an InventoryToSerialNumberTransformer file to transform a Serial number into an inventory so that the InventoryType field only contains the serial number.

// src/Form/Type/InventoryType.php
$builder->add('serialNumber', TextType::class, ['label' => 'Serialnumber'])
// src/Form/Type/SaleDetailsType.php
$builder->add('Inventory', InventoryType::class, ['label' => 'Inventory item']);
// src/Form/Type/SaleType.php
$builder->add('account', EntityType::class, [ 'class' => Account::class ])
    ->add('shop', EntityType::class, [ 'shop' => Shop::class ])
    ->add('saledetails', CollectionType::class, [ 'entry_type' => SaleDetailsType::class, 
                                                  'entry_options' => ['label' => false],
                                                  'allow_add' => true ])
    ->add('save', SubmitType::class, ['label' => 'Create Sale!']);
{{ form_start(form) }}

{{ form_row(form.account) }}
{{ form_row(form.shop) }}

<h3>Saledetails</h3>
<ul class="saledetails">
    {% for detail in form.saledetails %}
        {% for inventory in detail %}
            <li> {{ form_row(inventory.serialNumber) }}</li>
        {% endfor %}
    {% endfor %}
</ul>

{{ form_row(form.save) }}

{{ form_row(form._token) }}

Expected behavior:

When adding list items via clientside JavaScript that is identical to the format that symfony uses (this is already done and works perfectly) the "inventory" should be correctly inserted just from the serial number alone.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据