Book Image

Symfony2 Essentials

Book Image

Symfony2 Essentials

Overview of this book

Table of Contents (17 chapters)
Symfony2 Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Form creation


Symfony2 uses the form component to make the creation of forms easier. The form component is a standalone library and can be used independently, but integration with Symfony2 is very good and seamless.

We have two ways to generate forms. The quicker one is to define forms directly within the controller. While this is the quickest way to create a form, it is not the most elegant one. Forms created in this way are not easy to refactor and are not reusable.

For example, to add a new task to our list, we could define the following form in our task controller in src/AppBundle/Controller/TaskController.php:

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Task;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class TaskController extends Controller
{
    public function listAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

        $tasks = $em->getRepository('AppBundle:Task...