Search code examples
symfonysymfony3

Error during form generation: Could not load type "CustomType"


There is an error during execute the createForm method.

InvalidArgumentException: Could not load type "ArticleType"

My symfony version is 3.3.*.

I tried to execute the createForm method with Article::class instead of ArticleType::class.

Here is my code, where is the problem?

ArticleController.php

public function createAction(Request $request)
{
    $article = new Article();

    $form = $this->createForm(ArticleType::class, $article);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // ...
    }

    return $this->render('admin/article/create.html.twig', [
        'form' => $form->createView()
    ]);
}

ArticleType.php

class ArticleType extends AbstractType
{
    private $categoryService;
    private $tagService;

    public function __construct(CategoryService $categoryService, TagService $tagService)
    {
        $this->categoryService = $categoryService;
        $this->tagService = $tagService;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */.
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // ...
    }

    public function setDefaultOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'CMS\Bundle\ContentBundle\Entity\Article'
        ]);
    }
}

Resources/config/services.yml (included in app/config/services.yml)

services:
  CMS\Bundle\ContentBundle\Form\ArticleType:
    arguments: ['@cms.core.service.category', '@cms.core.service.tag']
    tags: [form.type]

.


Solution

  • It looks like your custom form class can't be found in the current namespace(s). Try adding use CMS\Bundle\ContentBundle\Form\ArticleType; (or something similar) to your controller.