gpt4 book ai didi

symfony - 创建一个简单的表单

转载 作者:行者123 更新时间:2023-12-02 10:12:00 25 4
gpt4 key购买 nike

只想在 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()
));
}
}

错误: frontend\mainBundle\Form\ContactForm::buildForm() 的声明必须与 ContactForm.php 第 8 行中的 Symfony\Component\Form\FormTypeInterface::buildForm(Symfony\Component\Form\FormBuilderInterface $builder, array $options) 兼容.

出了什么问题?

抱歉,习惯了 symfony 1.4

最佳答案

实际上,该错误与 symfony 无关。发生这种情况是因为 buildForm 函数的声明与基类(AbstractType)的声明不匹配。 buildForm 的第一个参数必须是 FormBuilderInterface 的实例,而您的第一个参数是 FormBuilder

的实例
// ...
use Symfony\Component\Form\FormBuilderInterface;

class ContactForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
// ...

关于symfony - 创建一个简单的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14012670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com