我有以下代码
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Todo;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
class ToDoController extends Controller
{
/**
* @Route("/todo", name="todo_list")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function listAction()
{
$todos = $this->getDoctrine()->getRepository('AppBundle:Todo')->findAll();
return $this->render('todo/index.html.twig', array(
'todos' => $todos
));
}
/**
* @Route("/todo/create", name="todo_create")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function createAction(Request $request)
{
$todo = new Todo();
$form = $this->createFormBuilder($todo)
->add('name', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('category', TextType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
->add('description', TextareaType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
die('Submitted');
}
return $this->render('todo/create.html.twig', array(
'form' => $form->createView()
));
}
/**
* @Route("todo/edit/{id}", name="todo_edit")
* @param $id
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function editAction($id, Request $request)
{
return $this->render('todo/edit.html.twig');
}
/**
* @Route("/todo/details/{id}", name="todo_details")
*/
public function detailsAction($id)
{
return $this->render('todo/details.html.twig');
}
}
问题是找不到像 handleRequest()isSubmitted()isValid()createView()等.. 这样的方法。我不知道为什么?谁能帮我 ?提前谢谢。
答案 0 :(得分:0)
你做的是
$form = $this->createFormBuilder($todo)
// ...
;
$form->handleRequest($request);
// ...
这是不正确的, 你需要做什么
$form = $this->createFormBuilder($todo)
// ...
->getForm()
;
// ...
找不到您的方法,因为createFormBuilder()
将返回FormBuilder
个对象,方法handleRequest()
以及其他对象位于对象Form
修改强>
删除您未找到方法定义的ide警告。你可以做到以下几点:
use Symfony\Component\Form\Form;
// ...
/** @var Form $form */
$form = $this->createFormBuilder($todo)
// ...
请注意,它是100%整容,警告不会影响代码执行。
我不知道为什么你的想法找不到建筑师所拥有的方法&gt; 1个字段。我可以100%重现这种错误的行为,但你的代码会起作用。
为了解决这些警告,您可以考虑using form classes
编辑2
如果您使用phpStorm的Symfony插件,他们的github上有similar issue