Symfony 2 Form

Featured Image

NOTE: Tested on Symfony2 PR12. Might not work on later releases!

If you want to use forms in Symfony 2 project there is already prepared system for that.

First in ‘Yourname/SomethingBundle/Forms’ folder create class like this:

namespace Surgeworks\AdminBundle\Forms;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\TextField;
use Symfony\Component\Form\HiddenField;
//form for Create and update information about languages
class LanguageForm extends Form{
    protected function configure() {
        $this->add(new TextField('language_name',  array('max_length'=>50, 'required' => true)));
        $this->add(new TextField('language_symbol',  array('max_length'=>10, 'required' => true)));
        $this->addOption('name', 'adminForm');

then the form is ready for using inside controller:

     * @extra:Route("/admin/languages/add_new", name="_admin_languages_add")
     * @extra:Template
    public function addAction() {
        $form = LanguageForm::create($this->get('form.context'), 'adminForm');

After that in the same controller action bind form to your existing Entity:

$request = $this->get('Request');
            $form->bind($request, $language);

Attention: make sure that form field names is same like your Entity properties names!

One little tip: When field names in Entity class contain _ (underscore) character, make sure to remove that character in setter and getter function names, for example:

//contains underscore character like fieldname in database
protected $status_id;
//theese methods must be without that character
    public function getStatusId() {
        return $this->status_id;
    public function setStatusId($status_id) {
        $this->status_id = $status_id;

For displaying form in the template there are several ways, end one of them is:

        <form action="#" method="post" id="adminForm" enctype="application/x-www-form-urlencoded">
    {{ form_errors(form) }}
    {% for field in form %}
        {% if not field.ishidden %}
            <div class="formitem">
            {{ form_errors(field) }}
            {{ form_label(field) }}
            {{ form_field(field) }}
        {% endif %}
    {% endfor %}
{{ form_hidden(form) }}
                <input type="submit" value="submit"/>

In the next article of Symfony 2 series I will write about validating form’s data in Symfony 2.


  1. Thanks for comment, but I would rather say that will be outdated in Symfony 2 beta release. I am working since PR9 and I am trying to keep fresh information online. Now i am waiting for beta and will write another article with new stuff. Also I mentioned that it is little outdated already in my last article:
    Anyway, thanks for reading and suggestion 🙂

  2. Stéphane, thanks for interesting in my article. I recently started with new project on Symfony 2 framework and I am learning ‘things’ as they come in my project. Most of the things are learned from official Symfony 2 site, but some I had to investigate and findout on my own. I also have to mention that I have also big support from my friend Branko Ajzele ( who is exploring Symfony 2 framework too.

  3. Hi Darko,

    Thanks for that introduction to Symfony2 forms ! I’m currently reading the official book, but on that subject, the documentation is clearly outdated. I can’t wait to get the next part of your article…

    By the way, where did you learn that way of doing things (mailing list, irc, another channel, …) ? Personnaly I’m just trying to understand the api by myself, but that’s certainly not a productive method…



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.