我们正在尝试从扩展,CustomerProfileType并且会出现如下错误:
{
"code": 500,
"message": "Could not load type "abc\Form\Extension\AdminApi\CustomerProfileTypeExtension": class does not implement "Symfony\Component\Form\FormTypeInterface"."
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
您是否通过传递您的实体来调用 createForm 方法?
如果是这样,那么您可能忘记插入为实体创建的 formType 类。这是我做错的地方。
就我而言,我写道 $form = $this->createForm(Article::class);
但代码应该是 $form = $this->createForm(ArticleFormType::class);
如异常消息所示,您正在实现表单类型扩展而不是表单类型。
表单类型扩展旨在修改表单功能的方式:
它们有2个主要用例:
您想要向单个表单类型添加特定功能(例如,向FileType字段类型添加“下载”功能);
您想要将通用功能添加到几种类型(例如,将“帮助”文本添加到每个类似于“输入文本”的类型)。
要实现特定的形式,您应该实现Symfony\Component\Form\FormTypeInterface或扩展实现它的类(在Symfony中通常是Symfony\Component\Form\AbstractType)。
要在表单类型中使用继承,请使用FormInterface#getParent。这样的问题可能会帮助您。
| 归档时间: |
|
| 查看次数: |
5038 次 |
| 最近记录: |