Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a simple question about Symfony2's form type "collection" in combination with doctrine's column "array".**

My Entity:

/**
 * @Column(type="array", nullable=true)
 * @var array
 */
private $specifications;

My "Main" Form:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('specifications', 'collection', array (
            'type' => new ProductSpecificationType(),
            'allow_add' => true,
            'allow_delete' => true,
            'by_reference' => false
        )
    );
}

My "ProductSpecificationsType" Form:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('group', 'text')
        ->add('info', 'text')
    ;
}

And my output with button (twig):

{{ form_row(form.specifications) }}
<a data-prototype="{{ form_widget(form.specifications.vars.prototype) | e }}" href="#" onclick="return false;">Add</a>

The real outout is this (html):

<a data-prototype="" href="#" onclick="return false;">Add</a>

And the same if I use only the form_widget without the "e" like "escape" at the end.

After save the entity is saved with the array column = null.

So, if I add these lines per FireBug manually to the html form

<input type="text" name="product[specifications][0][group]" value="Exam">
<input type="text" name="product[specifications][0][info]" value="ple">
<input type="text" name="product[specifications][1][group]" value="Extra">
<input type="text" name="product[specifications][1][info]" value="Example">

and press then "Save", the entity is saved in this format:

a:2:{i:0;a:2:{s:5:"group";s:4:"Exam";s:4:"info";s:3:"ple";}i:1;a:2:{s:5:"group";s:5:"Extra";s:4:"info";s:7:"Example";}}

My questions:

  1. Why is my prototype empty?

  2. Why is the form_widget empty? (is also empty if I fill the data above manually!)

Thank you in advance!

share|improve this question
try var_dumping your 'main' form_builder and see what is there. (e.g. var_dump($builder);die;) – Lighthart Apr 19 at 21:19

1 Answer

Have you tried adding 'prototype' => true to the "Main" form builder? Something like:

$builder->add('specifications', 'collection', array (
        'type' => new ProductSpecificationType(),
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false,
        'prototype' => true
    )
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.