0

执行createForm方法时出错。

InvalidArgumentException: Could not load type "ArticleType"

我的 symfony 版本是3.3.*.

我尝试使用而不是执行该createForm方法。Article::classArticleType::class

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

文章控制器.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()
    ]);
}

文章类型.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]

.

4

1 回答 1

1

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

于 2017-10-10T11:37:28.663 回答