Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - default values in Form Collection prototype

Tags:

symfony

I'm trying to get the collection prototype to have a set of default values instead of blank values. Ideally I'd like to be able to define those default somewhere either in the model class or the form definition class, but I cannot find a way to do this anywhere.

As an example:

I've created an AbstractType for my form which contains a nested collection of Person rows (relevant code shown below):

public function buildForm(FormBuilder $builder, array $options)
{
    ...
    $builder->add('people', 'collection', array(
            'type'         => new PersonType(),
            'allow_add'    => true,
            'allow_delete' => true,
            'prototype'    => true,
        ));
    ...
}

The PersonType class contains the following code:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('name', 'text');
    $builder->add('date_of_birth', 'date');
    $builder->add('age', 'number');

    // This would be great if I could do this but I can't:
    //$builder->add('date_of_birth', 'date', array('empty_value' => new \DateTime(...))); // some default value defined here
}

The best I've been able to come up with so far is shown below in the view file (the code shown is used to render the collection prototype):

...
<tr>
    <td> {{ form_widget(person.name) }} </td>

    {# THIS DOES NOT WORK (I just get the default selected date) #}
    <td> {{ form_widget(person.date_of_birth, {'value': person.date_of_birth.get('value')|default({'year':2010, 'month':10, 'day':15})} }} </td>

    {# THIS WORKS (the field contains '0' instead of being empty) #}
    <td> {{ form_widget(person.age, {'value': person.age.get('value')|default(0)} }} </td>
</tr>
...
  • It only seems to work with simple types like text and number. It doesn't work with the date type.
  • This anyway doesn't feel like the right approach. I should be able to define a default/empty value either in the underlying model (e.g. protected $age = 10; inside the model class), or else in the form definition (AbstractType) class (e.g. array('empty_value' => new DateTime()), but neither are currently possible.

So in summary, my question is:

How can I define default values for a model class that will be set automatically on the client when adding new items to a form 'collection' (instead of just getting blanks).

Does anyone know of a good way to do this?

like image 397
agentar Avatar asked Sep 21 '11 16:09

agentar


1 Answers

In the constructor of the entity that the form is being used for, you simply set the date with a \DateTime object, like so:

class MyEntity {
    private $myDate;

    public function __construct() {
        $this->myDate = new \DateTime('today'); 
    }
}

You can also use \DateTime('now') or \DateTime('tomorrow'), as described in the discussion below

http://groups.google.com/group/symfony2/browse_thread/thread/18a5b20aca485dc4/e9947d0f06d6519d

Edit: Actually, this information is in the symfony2 documentation:

http://symfony.com/doc/2.0/book/forms.html#building-the-form

like image 62
kdazzle Avatar answered Nov 19 '22 02:11

kdazzle