我刚刚将symfony从2.7更新到3.0并且遇到了一些问题..
它无法加载我的表单类型.这是一个例子.
的services.xml
app.search:
class: AppBundle\Form\Type\SearchFormType
tags:
- { name: form.type, alias: app_search }
Run Code Online (Sandbox Code Playgroud)
多数民众赞成我试图创造形式.
$form = $this->createForm('app_search', new Search());
Run Code Online (Sandbox Code Playgroud)
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';
}
}
Run Code Online (Sandbox Code Playgroud)
得到下一个错误:
在...中呈现模板("无法加载类型"app_search"")期间抛出异常
在symfony 3.0中应该如何?
谢谢 !
您应该将设置更改为..
app.search:
class: AppBundle\Form\Type\SearchFormType
tags:
- { name: form.type }
Run Code Online (Sandbox Code Playgroud)
并在您的表单类型
use Symfony\Component\Form\Extension\Core\Type\TextType;
// ...
$builder->add('phrase', TextType::class);
Run Code Online (Sandbox Code Playgroud)
然后叫它用...
$form = $this->createForm(SearchFormType::class, new Search());
// or $form = $this->createForm('AppBundle\Form\Type\SearchFormType', new Search());
Run Code Online (Sandbox Code Playgroud)
它的长短是表单不再被命名,它们由类名引用.
今天我被卡住了大约2个小时.问题只来自表单类型的命名空间.写,而不是namespace AppBundle\Form\Type;在你的SearchFormType.php文件中使用namespace AppBundle\Form;.
| 归档时间: |
|
| 查看次数: |
15427 次 |
| 最近记录: |