Symfony 3-无法加载类型表单类型


Symfony 3 - Could not load type form type

我刚刚将symfony从2.7更新到3.0,并遇到了一些问题。

它无法加载我的表单类型。下面是一个例子。

services.xml

app.search:
        class: AppBundle'Form'Type'SearchFormType
        tags:
            - { name: form.type, alias: app_search }

这就是我试图创建表单的方式。

$form = $this->createForm('app_search', new Search());

SearchFormType

namespace AppBundle'Form'Type;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
class SearchFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder->add('phrase', 'text');
    }
    public function getBlockPrefix()
    {
        return 'app_search';
    }
}

获取下一个错误:

在呈现模板期间引发异常("无法在…中加载类型"app_search"(。。。。

它在symfony 3.0中应该是什么样子?

谢谢!

您应该将设置更改为..

app.search:
    class: AppBundle'Form'Type'SearchFormType
    tags:
        - { name: form.type }

在你的表格中键入

use Symfony'Component'Form'Extension'Core'Type'TextType;
    // ...
    $builder->add('phrase', TextType::class);

然后称之为使用。。。

$form = $this->createForm(SearchFormType::class, new Search());
// or $form = $this->createForm('AppBundle'Form'Type'SearchFormType', new Search());

总之,表单不再命名,而是由类名引用。

我今天被这个问题困扰了大约2个小时。问题只是来自表单类型的名称空间。不要在SearchFormType.php文件中使用namespace AppBundle'Form'Type;,而是编写namespace AppBundle'Form;