【问题标题】:Class does not implement "Symfony\Component\Form\FormTypeInterface" in Sylius类未在 Sylius 中实现“Symfony\Component\Form\FormTypeInterface”
【发布时间】:2018-06-26 06:49:35
【问题描述】:

我们正在尝试从 CustomerProfileType 扩展,但我们收到如下错误:

 {
"code": 500,
"message": "Could not load type "abc\Form\Extension\AdminApi\CustomerProfileTypeExtension": class does not implement "Symfony\Component\Form\FormTypeInterface"."
}

客户.yml:

sylius_admin_api_customer_create:
    path: /
    methods: [POST]
    defaults:
        _controller: sylius.controller.customer:createAction
        _sylius:
            serialization_version: $version
            serialization_groups: [Detailed]
            form:
                type: abc\Form\Extension\AdminApi\CustomerProfileTypeExtension

CustomerProfileTypeExtension.php

final class CustomerProfileTypeExtension extends AbstractTypeExtension
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        // Adding new fields works just like in the parent form type.
        $builder->add('contactHours', TextType::class, [
            'required' => false,
            'label' => 'app.form.customer.contact_hours',
        ]);

        // To remove a field from a form simply call ->remove(`fieldName`).
        $builder->remove('gender');

        // You can change the label by adding again the same field with a changed `label` parameter.
        $builder->add('lastName', TextType::class, [
            'label' => 'app.form.customer.surname',
        ]);
    }


    /**
     * {@inheritdoc}
     */
    public function getExtendedType(): string
    {
        return CustomerProfileType::class;
    }


}

【问题讨论】:

    标签: symfony sylius


    【解决方案1】:

    您是否通过传递实体来调用 createForm 方法?

    如果是这样,那么您可能会忘记插入为实体创建的 formType 类。这是我做错的地方。

    就我而言,我写了$form = $this->createForm(Article::class);

    但代码应该是$form = $this->createForm(ArticleFormType::class);

    【讨论】:

      【解决方案2】:

      正如异常消息所暗示的,您正在实现表单类型扩展而不是表单类型。

      表单类型扩展are intended to modify the way forms function:

      它们有两个主要用例:

      您想为单个表单类型添加特定功能(例如向 FileType 字段类型添加“下载”功能);

      您想为多种类型添加通用功能(例如为每个类似“输入文本”的类型添加“帮助”文本)。

      要实现特定的表单,您应该实现 Symfony\Component\Form\FormTypeInterface 或扩展实现它的类(通常在 Symfony 中为 Symfony\Component\Form\AbstractType)。

      要在表单类型中使用继承,请使用FormInterface#getParentThis SO question 可能会帮助你。

      【讨论】:

        猜你喜欢
        • 2015-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-17
        • 2017-04-19
        • 1970-01-01
        相关资源
        最近更新 更多