首页 > 解决方案 > 问:如何在结帐中添加自定义表单字段?

问题描述

我一直在尝试将自定义表单字段添加到交付方式的结帐步骤,客户可以在其中选择交付日期。

我已将 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"

标签: phpsymfonysylius

解决方案


推荐阅读