我们正尝试从CustomerProfileType
开始扩展,并且出现类似以下错误:
{
"code": 500,
"message": "Could not load type "abc\Form\Extension\AdminApi\CustomerProfileTypeExtension": class does not implement "Symfony\Component\Form\FormTypeInterface"."
}
Customer.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;
}
}
答案 0 :(得分:5)
您通过传递您的实体来调用createForm方法吗?
如果是这样,则您可能会忘记插入为实体创建的formType类。这是我做错事的地方。
就我而言,我写了$form = $this->createForm(Article::class);
但代码应为$form = $this->createForm(ArticleFormType::class);
答案 1 :(得分:3)
如异常消息所示,您正在实现表单类型扩展而不是表单类型。
表单类型扩展名are intended to modify the way forms function:
它们有2个主要用例:
您要向单个表单类型添加特定功能(例如,向FileType字段类型添加“下载”功能);
您想为几种类型添加通用功能(例如,向每个类似于“输入文本”的类型添加“帮助”文本)。
要实现特定形式,您应该实现Symfony\Component\Form\FormTypeInterface
或扩展实现它的类(在Symfony中通常为Symfony\Component\Form\AbstractType
)。
要在表单类型中使用继承,请使用FormInterface#getParent
。 This SO question可能会帮助您。