Dead simple default values on Symfony2 forms

Last day I wanted to set the default values for text and choice  type fields in a project and it came out that I couldn’t find any reference about this in the docs.  While digging on other  blogs for tips I found various methods but they all required an entity for it!! (I felt that this was an overkill solution for what I wanted to do).

I just needed to add a simple text and select (choice) field that really had not enough semantic meaning to justify creating a new entity each.

So my first attempt was trying to set it on Type definition like this:

$builder
->add('name', 'text', array(
    'attr' => array('value' => '<type a name>')    
))
->add('favNumber', 'choice', array(
    'choices' => array(0, 1, 2, 3, 4, 5)
));

Then on the controller

$entity = new MyEntity();
$form   = $this->createForm(new MyEntityType(), $entity);

I expected this to work as with labels and set the default value to “<type a name>” when the entity didn’t had a value and if it did then use the entity’s value.  And it did somewhat worked but the problem with this approach is that it always sets the value to “<type a name>” regardless if the entity had a value or not.  So for the edit form this wasn’t going to cut it, and using javascript  on document load() for this was unacceptable.  Also similar approach didn’t worked at all for choice fields.

So after a while I found that simply setting the default value on the Entity class worked well for both text and choice types.

// Entity
class Entity {
    private $name = '<type a name>';
    private $favNumber = '3';
    ...
 }

// Type
$builder
->add('name', 'text')
->add('favNumber', 'choice', array(
    'choices' => array(0, 1, 2, 3, 4, 5)
));

// Controller
$entity = new MyEntity();
$form   = $this->createForm(new MyEntityType(), $entity);

AHA! and this works as expected on edit forms as well.

// EXTRA
Sometimes you need to provide default values different than the defaults ones (you know what I mean), in that case (except for choice types) twig provides a convenient way to work this out:

{{ form_widget(form.name, {'attr': {'value': '<Full Name>'}}) }}

You can use this method to override any attribute or check checkboxes.

And that’s it, hope this prove useful and helps you become more productive.

7 thoughts on “Dead simple default values on Symfony2 forms

  1. Apart from setting the default in the model, you can also pass the “data” option when adding a field or call setData() on the field once the form is created.

    At last, instead of passing the “value” *attribute* you should pass the “value” *variable* in the template, which is sort of a more generic version of the attribute:

    {{ form_widget(form.name, {‘value’: ”}) }}

    • Bernhard, thanks for you comment. I agree a variable should be used instead of hardcoded values for a number of reasons. (but its clearer for the example to use a literal string 😉 )

      Would you share a snippet on how to use setData() on ‘favNumber’ field to set the default value? If its possible that’s definitely the way to go.

      Cheers,

Leave a Reply

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