表单生成期间出错:无法加载类型" CustomType"

时间:2017-10-10 10:35:17

标签: symfony symfony3.x

执行createForm方法时出错。

InvalidArgumentException: Could not load type "ArticleType"

我的symfony版本是3.3.*

我尝试使用createForm代替Article::class执行ArticleType::class方法。

这是我的代码,问题出在哪里?

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 (包含在app / config / services.yml中)

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

1 个答案:

答案 0 :(得分:1)

看起来您的自定义表单类无法在当前名称空间中找到。尝试将use CMS\Bundle\ContentBundle\Form\ArticleType;(或类似的东西)添加到您的控制器。