7. Forms

Symfony2 integrates a Form component that makes dealing with forms easy. In this chapter, you’ll see how to render Symfony2 forms in Smarty templates. Oh yeah!

Warning

Form support in SmartyBundle is currently under development and it is expected to be broken here and there. Please be patient and don’t be shy to share your experiences with this extension. It will help us improve it. Thanks!

Also, only Form components released in Symfony 2.1.0 or higher are supported.

7.1. Rendering a Form

First you need to create a form instance as described in Creating a Simple Form.

// src/Acme/TaskBundle/Controller/DefaultController.php
namespace Acme\TaskBundle\Controller;

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

class DefaultController extends Controller
{
    public function newAction(Request $request)
    {
        // create a task and give it some dummy data for this example
        $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('AcmeTaskBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

Once you create a form instance, the next step is to render it. This is done by passing a special form “view” object to your template (notice the $form->createView() in the controller above) and using a set of form helper functions:

{* src/Acme/TaskBundle/Resources/views/Default/new.html.smarty *}
<form action="{'task_new'|path}" method="post" {form_enctype form=$form}>
    {form_widget form=$form}

    <input type="submit" />
</form>

More examples coming soon...