Class does not implement "Symfony\Component\Form\FormTypeInterface" in Sylius

stefun picture stefun · Jun 26, 2018 · Viewed 10.5k times · Source

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;
    }


}

Answer

sh6210 picture sh6210 · Apr 18, 2020

Are you calling createForm method by passing your entity ?

If so then you might forget to insert the formType class you created for the entity. This is where i was doing mistake.

in my case i wrote $form = $this->createForm(Article::class);

but the code should be $form = $this->createForm(ArticleFormType::class);