Skip to content Skip to sidebar Skip to footer

Symfony 2.3.6 Nested Forms

I am trying to have a form with a collection of forms that will allow me to fill in weekly data. I have an Entity that is for the week with a few stats /** * @ORM\Column(type='int

Solution 1:

Yes you can, look at the documentation, if you add seven DailyStats entities to your week entity then symfony2 will render those seven inputs that you want, please check http://symfony.com/doc/current/cookbook/form/form_collections.html

class TaskController extends Controller
{   
      public function newAction(Request $request)
      {
           $task = new Task();

           // dummy code - this is here just so that the Task has some tags
           // otherwise, this isn't an interesting example
           $tag1 = new Tag();
           $tag1->name = 'tag1';
           $task->getTags()->add($tag1); // any new related entity you add represents a new embeded form
           $tag2 = new Tag();
           $tag2->name = 'tag2';
           $task->getTags()->add($tag2);
           // end dummy code

           $form = $this->createForm(new TaskType(), $task);

           $form->handleRequest($request);

           if ($form->isValid()) {
               // ... maybe do some form processing, like saving the Task and Tag objects
           }

           return $this->render('AcmeTaskBundle:Task:new.html.twig', array(
               'form' => $form->createView(),
           ));
     }
}

Post a Comment for "Symfony 2.3.6 Nested Forms"