我正在创建一个简单的联系表。我的自定义类型以这种方式开始:
<?php
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\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
...并且我的控制器以这种方式启动:
<?php
namespace MyCompany\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class ContactController extends Controller
{
public function contactAction(Request $request)
{
// Create the form according to the FormType created previously.
// And give the proper parameters
$form = $this->createForm('MyCompany\AppBundle\Form\ContactType',null,array(
// To set the action use $this->generateUrl('route_identifier')
'action' => $this->generateUrl('myapplication_contact'),
'method' => 'POST'
));
...但是我发现我的应用程序无法加载类型。我收到以下异常消息:
无法加载类型“ MyCompany \ AppBundle \ Form \ ContactType” 500内部 服务器错误-InvalidArgumentException
我该怎么做才能开始调试呢?
答案 0 :(得分:2)
由于您使用的是Symfony 2.7,因此使用表单类型类在控制器中创建表单的正确方法是:
<?php
namespace MyCompany\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use MyCompany\AppBundle\Form\ContactType; // add this
class ContactController extends Controller
{
public function contactAction(Request $request)
{
// Create the form according to the FormType created previously.
// And give the proper parameters
$form = $this->createForm(new ContactType(), null, array(
// To set the action use $this->generateUrl('route_identifier')
'action' => $this->generateUrl('myapplication_contact'),
'method' => 'POST'
));
答案 1 :(得分:1)
在Symfony 2.7中,您必须在$this->createForm
new MyCompany\AppBundle\Form\ContactType