Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

How Symfony2 Forms Works?

Basic usage
Built-in Field Types
Text Fields Choice Fields Date and Time Field Groups
Fragments Templates
The fragments are defined as blocks in Twig and as template files in PHP. {% block field_row %}

TWIG
text choice Fields collection <div class="form_row">
textarea entity date repeated label (e.g. field_label) renders the field's label {{ form_label(form) }}
email country datetime widget (e.g. field_widget)renders the field's HTML representation {{ form_errors(form) }}
Data is Valid Data is invalid integer language time Hidden Fields errors (e.g. field_errors)renders the field's errors {{ form_widget(form) }}
money locale birthday hidden row (e.g. field_row) renders the field's entire row </div>
Form is bound and you can Form is bound and
for e.g. persist data to DB rendered, displaying number timezone csrf (label, widget & errors) {% endblock field_row %}
before redirecting the user all validation errors password Other Fields rows (e.g. field_rows) renders all form field rows
to other page percent checkbox Base Fields rest (e.g. form_rest) renders any fields that have not yet been rendered
search file field enctype (e.g. form_enctype) if at least one field is a file upload field, this
url radio form renders the enctype="multipart/form-data”

Entity represent and


Controller The Form
store the data Validation
Applied to the Task Write a blog post
underlying object (class)
src/Acme/TaskBundle/Entity/Task.php src/Acme/TaskBundle/Controller/DefaultController.php Jul 24 , 2011
by adding a set of Duedate
rules (constraints).
These rules can $task = new Task(); Submit
namespace Acme\TaskBundle\Entity;
be specified in $task->setTask('Write a blog post');
YAML, XML, $task->setDueDate(new \DateTime('tomorrow'));
use Symfony\Component\Validator\Constraints as Assert; annotations, Create
or PHP the form
class Task{ $form = $this->createFormBuilder($task) render
/** ->add('task', 'text') Built-in Field the
* @Assert\NotBlank() using annotations ->add('dueDate', 'date') Types form
*/ for constraints bind the submitted data to ->getForm();
public $task; the form, which translates
that data back to the task
and dueDate properties of if ($request->getMethod() == 'POST') {
/**
* @Assert\NotBlank()
the $task object
$form->bindRequest($request);
View
* @Assert\Type("\DateTime") ask the $task object whether
or not it has valid data
*/ if ($form->isValid()) {
protected $dueDate; src/Acme/TaskBundle/Resources/views/Default/new.html.twig
// perform some action, such as saving the task to DB
return $this->redirect($this->generateUrl('task_success')); <form action="{{ path('task_new') }}" method="post"

TWIG
public function getTask() { {{ form_enctype(form) }}>
}
return $this->task; {{ form_widget(form) }}
} }
public function setTask($task) { Unless a property is <input type="submit" />
$this->task = $task; public, it must have </form>
return $this->render('AcmeTaskBundle:Default:new.html.twig',
} a "getter" and "setter"
method so that the array( OR
form component can 'form' => $form->createView(), src/Acme/TaskBundle/Resources/views/Default/new.html.php
public function getDueDate() { get and put data onto
return $this->dueDate; the property ));
<form action="<?php echo $view['router']->generate('task_new') ?>"
}

PHP
method="post" <?php echo $view['form']->enctype($form) ?> >
public function setDueDate(\DateTime $dueDate = null) { <?php echo $view['form']->widget($form) ?>
$this->dueDate = $dueDate; bindRequest isValid createView
} translate user-submitted check for valid render the
data back to the data in the form <input type="submit" />
} properties of an object object </form>

Examples from: http://symfony.com/doc/current/book/forms.html andreiabohner.wordpress.com

You might also like