执行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]
答案 0 :(得分:1)
看起来您的自定义表单类无法在当前名称空间中找到。尝试将use CMS\Bundle\ContentBundle\Form\ArticleType;
(或类似的东西)添加到您的控制器。