Johni
Johni

Reputation: 2961

Symfony 2 dynamic embedded forms, wrong (entity) type

I have two entities with a on to many relationship.

One "Entry" has n "Tags". Now I want to insert a new Entry with Tags. The form consist of a collection of "Tag" forms which can be dynamically added via javascript. One field is added by default in the controller code.

When the form gets submittet and I iterate over the Tags for the Entry, only the first one (which has been added in the controller, not via javascript) has the proper entity type. The other ones are just arrays:

object(Entity\Type)[88]
   private 'id' => null
   private 'field' => null
   private 'type' => string 'S' (length=1)

array (size=3)
  'field' => null
  'type' => string 'S' (length=1)

The "Entry" controller:

$entity = new Entry();
$entity->getParameters()->add(new EntryTag());

$form = $this->createForm($this->get('entryform'),$entity);

if($request->getMethod() == 'POST'){
    $form->bindRequest($request);

    $formData = $form->getData();
    foreach ($formData->getParameters() as $par) {
        var_dump($par);
    }
}

The "Entry" type: $this->tagType gets injeced in the constructor.

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

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Entity\Entry',
    ));
}

The "Tag" type:

public function buildForm(FormBuilder $builder, array $options){
    $builder->add('field', 'text', array('label' => 'Key'));
    $builder->add('type', 'choice', array('label' => 'Type', 'required' => false, 'choices' => $this->getTypes()));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
            'data_class' => 'Entity/Tag',
    ));
}

The javascript i use to add the fields:

var $childs = $container.find('.form-child');

fieldIndindex++; 

var template = $container
            .addClass('form-child')
    .attr('data-prototype')
    .replace(/\$\$name\$\$/g, fieldIndindex);

if($childs.length == 0){
    $container.prepend($template);
}else{
    $childs.last().after($template);
}

So, why isn't the entity typ detected?

Edit:

When i add the "data_class" option to my "collection" field, it works.

$builder->add('parameters', 'collection', array(
        'type' => $this->parameterType,
        'allow_add' => true,
        'allow_delete' => true,
        'by_reference' => false,
        'options' => array(
            'data_class' => 'Entity/Tag',
        )
    ));

But setting it as default option in my "Tag" type should be enought. But why isn't it used?

Upvotes: 0

Views: 1348

Answers (1)

Johni
Johni

Reputation: 2961

I got it.....

I was using the Symfony 2.1 syntax in 2.0. After changing setDefault options to getDefaultOptions, it now works.

Upvotes: 1

Related Questions