Symfony表单问题-可变表单不存在

时间:2019-03-09 15:43:15

标签: php symfony

我正在使用symfony 4构建一个Crud应用程序。这是控制器代码:

<?php

namespace App\Controller;

 use App\Entity\Taskslist;
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
 class TodoController extends Controller{


/**

*   @Route("/",name="todo_list")


*/
public function listAction(){
   $todos=$this->getDoctrine()->getRepository('App:Taskslist')->findAll();
    return $this->render('todo/index.html.twig',array('todos' => $todos));

}
/**

*   @Route("/todo/create",name="todo_create")


*/

public function createAction(Request $request){


           $todo = new Taskslist;

        $form = $this->createFormBuilder($todo)
        ->add('id', TextType::class)
        ->add('title', TextType::class)
        ->add('description', TextareaType::class)
        // ->add('priority', ChoiceType::class, array('choices' => array('Low' => 'Low', 'Normal' => 'Normal', 'High'=>'High'), 'attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
       // ->add('status', BooleanType::class, array('attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
        ->add('Save', SubmitType::class, array('label'=> 'Create Todo'))
        ->getForm();

        $form->handleRequest($request);
        if($form->isSubmitted() &&  $form->isValid()){
            $name = $form['id']->getData();
            $category = $form['title']->getData();
            $description = $form['description']->getData();
            //$due_date = $form['status']->getData();
            //$name = $form['name']->getData();

            //$now = new\DateTime('now');  

            $todo->setId($name);
            $todo->setTitle($category);          
            $todo->setDescription($description);                  


            $sn = $this->getDoctrine()->getManager();      
            $sn -> persist($todo);
            $sn -> flush();


            return $this->redirectToRoute('todo_list');            

        }

        return $this->render('todos/create.html.twig', array(
            'form' => $form->createView()

        ));

}
/**

*   @Route("/todo/edit/{id}",name="todo_edit")


*/
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');

}

 }

这是edit.html.twig:

  {% extends 'base.html.twig' %}
  {% if form is defined %}

    {% block body %}

    {{form_start(form)}}
    {{form_widget(form)}}
    {{form_end(form)}}

    {% endblock %}
    {% endif %}

我遵循了所有教程,但是单击“编辑”按钮时出现此错误:

  

变量形式不存在。

尽管我的代码进入了if语句。 您能帮我吗?谢谢,我找不到任何可行的解决方案

1 个答案:

答案 0 :(得分:0)

我认为问题出在这里

{% extends 'base.html.twig' %}

{% if form is defined %}
{% block body %}
{{form_start(form)}}
{{form_widget(form)}}
{{form_end(form)}}
{% endblock %}
{% endif %}

改为使用此:

{% extends 'base.html.twig' %}

{% block body %}
    {{form_start(form)}}
    {{form_widget(form)}}
    {{form_end(form)}}
{% endblock %}