问题
We are trying to extend from CustomerProfileType
and we are getting error like:
{
"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;
}
}
回答1:
As the exception message suggests, you are implementing a form type extension instead of a form type.
Form type extensions are intended to modify the way forms function:
They have 2 main use-cases:
You want to add a specific feature to a single form type (such as adding a "download" feature to the FileType field type);
You want to add a generic feature to several types (such as adding a "help" text to every "input text"-like type).
To implement specific forms, you should either implement the Symfony\Component\Form\FormTypeInterface
or extend a class that implements it (commonly in Symfony that would be Symfony\Component\Form\AbstractType
).
To use inheritance in form types, use FormInterface#getParent. This SO question might help you with that.
来源:https://stackoverflow.com/questions/51036516/class-does-not-implement-symfony-component-form-formtypeinterface-in-sylius