我正在开发基于Symfony 2.7的应用程序。我有一个包含以下代码的自定义表单类型:
namespace MyCompany\AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\NotBlank;
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class,
[
'attr'=>
[
'placeholder' => 'Your name'
],
'constraints' =>
[
new NotBlank(['message' => 'Please provide your name'])
]
]
)
;
}
...
...,当我加载表单时,得到以下InvalidArgumentException:
无法加载类型 “ Symfony \ Component \ Form \ Extension \ Core \ Type \ TextType”
我已验证TextType类是否存在。
我尝试使用composer dump,但似乎没有帮助。另外,我尝试删除了供应商目录并重做了作曲家的安装,但这也无济于事。
我还能尝试什么?
答案 0 :(得分:3)
您不能使用完全限定的类名来表示Symfony v2.7中的表单类型,该类型是在v2.8中添加的。您需要通过传递实例来表示类型:
$builder
->add('name', new TextType(),
[
'attr' =>
[
'placeholder' => 'Your name',
],
'constraints' =>
[
new NotBlank(['message' => 'Please provide your name']),
],
]
);
或者使用shorthand name例如text
:
$builder
->add('name', 'text',
[
'attr' =>
[
'placeholder' => 'Your name',
],
'constraints' =>
[
new NotBlank(['message' => 'Please provide your name']),
],
]
);
虽然不再维护Symfony v2.7,所以我强烈建议至少升级到v2.8