Symfony2 form not showing

I am learning symfony2 and I created one form in the controller, which is below. controller file as DefaultController.php

namespace Banner\TestBundle\Controller; use Banner\TestBundle\Entity\Contact; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Banner\TestBundle\Entity\Task; use Symfony\Components\HttpFoundation\Request; class DefaultController extends Controller { public function newAction(Request $request) { echo "The controller is called "; $task = new Task(); $task->setTask("Write a blog Post "); $task->setDueDate(new DateTime('tomorrow')); $form = $this->createFormBuilder($task) ->add('task','text ') ->add('duedate','date') ->getForm(); return $this->render('BannerTestBundle:default:zoo.html.twig',array('form'=>$form->createView())); } } 

my routing file is below. routing.yml

 task_new: pattern: /task/{request} defaults: { _controller: BannerTestBundle:Default:new} 

and the zoo.html.twig file is below.

 {% extends '::page.html.twig' %} {% block title %}The Zoo{% endblock %} {% block content %} <form action="{{ path('task_new') }}" method="post" {{ form_enctype(form) }}> {{ form_widget(form) }} <input type="submit"> </form> {% endblock %} 

when I pass "task / GET" in my url, it will show that "Request does not exist." error 500 '.

what I basically want to do is when I pass the url zoo.html.twig. I want to show the form in zoo.html.twig.

+4
source share
2 answers

You do not need to pass $request to your action, and you do not need to enter it into your route. The request object is available in a global context. So

 task_new: pattern: /task-new defaults: { _controller: BannerTestBundle:Default:new} 

You can either leave it that way

 public function newAction(Request $request) 

or

 public function newAction() 

and an access request object like this

 $request = $this->getRequest(); 
+6
source

Your routing file is not properly indented, so yaml will not parse correctly. Try:

 task_new: pattern: /task/{request} defaults: { _controller: BannerTestBundle:Default:new} 

also:

  • It is probably a bad idea to redefine the request this way.
  • It seems that you are not doing anything with the request in the controller.
+1
source

All Articles