php - 问:如何在结帐中添加自定义表单字段?
问题描述
我一直在尝试将自定义表单字段添加到交付方式的结帐步骤,客户可以在其中选择交付日期。
我已将 delivery_date 值添加到 Shipment,它显示在转储日志和 mysql 中。
我写了一个像这样的 ShipmentTypeExtension 文件
<?php
namespace AppBundle\Form\Extension;
use Sylius\Bundle\ShippingBundle\Form\Type\ShipmentType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use AppBundle\Entity\Shipment;
final class ShipmentTypeExtension extends AbstractTypeExtension
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('delivery_date', DateType::class, [
'required' => true,
'label' => 'delivery_date',
]);
}
/**
* {@inheritdoc}
*/
public function getExtendedType(): string
{
return ShipmentType::class;
}
}
我在 AppBundle/Resources/config 中添加了一个 services.yml
services:
app.form.extension.type.shipment:
class: AppBundle\Form\Extension\ShipmentTypeExtension
tags:
- { name: form.type_extension, extended_type: Sylius\Bundle\ShippingBundle\Form\Type\ShipmentType }
并将这部分添加到我的 config.yml
sylius_shipping:
resources:
shipment:
classes:
model: AppBundle\Entity\Shipment
我覆盖了 _shipment.html.twig 并在 for 循环之后添加了这一行,它输出所有交付方法
{% include '@SyliusShop/Checkout/SelectShipping/_deliveryTimePicker.html.twig' %}
所以最后,我可以在我的新 _deliveryTimePicker.html.twig 中尝试添加一个 datepicker 字段。
但它不起作用,我如何正确呈现表单?
这是我的树枝转储():
array:17 [▼
"configuration" => RequestConfiguration {#25119 ▶}
"metadata" => Metadata {#4881 ▶}
"resource" => Order {#15648 ▶}
"order" => Order {#15648 ▶}
"form" => FormView {#37012 ▼
+vars: array:24 [▼
"value" => Shipment {#35552 ▶}
"attr" => []
"form" => FormView {#37012}
"id" => "sylius_checkout_select_shipping_shipments_0"
"name" => "0"
"full_name" => "sylius_checkout_select_shipping[shipments][0]"
"disabled" => false
"label" => null
"label_format" => null
"multipart" => false
"block_prefixes" => array:3 [▶]
"unique_block_prefix" => "_sylius_checkout_select_shipping_shipments_entry"
"translation_domain" => null
"cache_key" => "_sylius_checkout_select_shipping_shipments_entry_sylius_checkout_shipment"
"errors" => FormErrorIterator {#37013 ▶}
"valid" => true
"data" => Shipment {#35552 ▶}
"required" => true
"size" => null
"label_attr" => []
"compound" => true
"method" => "POST"
"action" => ""
"submitted" => false
]
+parent: FormView {#37008 ▶}
+children: array:1 [▶]
-rendered: false
-methodRendered: false
}
"wrap_fields_with_addons" => true
"app" => AppVariable {#23784 ▶}
"sylius" => ShopperContext_d398ef6 {#4770 ▶}
"sylius_base_locale" => "de"
"sylius_meta" => array:1 [▶]
"sonata_block" => GlobalVariables {#24160 ▶}
"shipment" => Shipment {#35552 ▼
#delivery_date: null
#order: Order {#15648}
#id: 4
#state: "cart"
#method: ShippingMethod {#35915 ▶}
#units: PersistentCollection {#35916 ▶}
#tracking: null
#createdAt: DateTime @1525251156 {#35547 ▶}
#updatedAt: DateTime @1525251157 {#35548 ▶}
}
"_parent" => array:11 [▶]
"_seq" => PersistentCollection {#17988 ▶}
"_iterated" => false
"loop" => array:8 [▶]
"_key" => 0
]
这是我得到的错误:
Neither the property "delivery_date" nor one of the methods "delivery_date()", "getdelivery_date()"/"isdelivery_date()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView"
解决方案
推荐阅读
- python - 使用 shell_exec() 在 php 中执行 conda 命令
- python - 从头开始为 Training ELMO Embedding 准备训练数据
- r - conditionalpanel and boxes width not giving expected output R shiny
- c++ - 编辑距离问题(使用 DP)——得到错误答案
- amazon-web-services - 在 EKS 上,如何验证我通过 Terraform 配置了 Spot 实例
- java - 春季在邮政编码处计算积极结果
- python - 在剧作家中按下“取消”弹出按钮会引发异常
- nginx - 规则禁止访问 - Nginx 日志记录
- scala - 如何定义一个 HList 类型,但基于另一个 HList 类型
- c# - 在 Linq 中,如何根据子记录进行查询?