只想在Symfony 2.1中创建一个简单的联系表单。怎么样?
ContactForm.php:
namespace frontend\mainBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ContactForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('subject', 'textarea');
$builder->add('email', 'email');
$builder->add('message', 'textarea');
}
public function getName()
{
return 'contact';
}
}
DefaultController.php
namespace frontend\mainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use mylib\DataFormatChecker;
use frontend\mainBundle\Form\ContactForm;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
public function contactAction(Request $request)
{
$task = array();
$form = $this->createFormBuilder(new ContactForm(), $task);
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
// perform some action, such as saving the task to the database
//return $this->redirect($this->generateUrl('task_success'));
}
}
return $this->render('frontendmainBundle:Default:contact.html.php', array(
'form' => $form->createView()
));
}
}
错误: 前缀\ mainBundle \ Form \ ContactForm :: buildForm()声明必须与ContactForm.php第8行中的Symfony \ Component \ Form \ FormTypeInterface :: buildForm(Symfony \ Component \ Form \ FormBuilderInterface $ builder,array $ options)兼容
有什么问题?
抱歉,习惯了symfony 1.4
答案 0 :(得分:4)
实际上,错误与symfony无关。这是因为buildForm
函数的声明与基类(AbstractType)的声明不匹配。 buildForm
的第一个参数必须是FormBuilderInterface
的实例,而您的第一个参数是FormBuilder
的实例
// ...
use Symfony\Component\Form\FormBuilderInterface;
class ContactForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
// ...