问:如何在结帐时添加自定义表单字段?

时间:2018-05-02 13:44:52

标签: php symfony sylius

我一直在尝试将自定义表单字段添加到投放方法的结帐步骤,客户可以在此处选择投放日期。

我已将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字段。

但它不起作用,我该如何正确渲染表单?

这是我的twig dump():

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"

0 个答案:

没有答案