开发者

Symfony2 - default values in Form Collection prototype

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 defi开发者_运维技巧ne 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?


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


may be

$builder->setData(array('date_of_birth', new \DateTime(...)));


With Symfony >2.0 this is not possible.

Symfony 2.0 retrieved the values for the prototype from the underlying object so setting them in the constructor also changed the values in the prototype. However, this behavior was changed with Symfony 2.1 which removed this functionality, depraving us of the possibility of setting default values for the prototype:

I think setting a default value for the prototype is then indeed not possible right now. --webmozart, Symfony collaborator (https://github.com/symfony/symfony/issues/5087)

There is an open bug under active development which should add support for a data_prototype option. Using this option, it will be possible to supply data to prefill the prototype. However, this will probably be released no sooner than with Symfony 2.7.


As @Alexey Kosov mentioned in his comment and @Chris mentioned was going to be possible, you can now set the option 'prototype_data' => new YourEntity()

$builder
    ->add('your_field', CollectionType::class, [
        'entry_type'     => YourEntityType::class,
        // ...
        'prototype_data' => new YourEntity(),
    ])
;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜