0

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?

6
  • Looks like your data_class might be wrong on your Tag Commented Oct 8, 2012 at 7:40
  • That's what i thought at first but I doublechecked it. I copied the namespace + classname to be sure. Commented Oct 8, 2012 at 7:59
  • Try making the data class the full path to the entity: 'data_class' => 'Acme\DemoBundle\Entity\Tag' - I suspect what might be happening is that the data_class isn't used when you setup the form in the controller - only when you start embedding the forms then it needs a "hint" on how to make the object out of the data Commented Oct 8, 2012 at 8:26
  • I already use the full namespace. I shortened it for the example code here. Commented Oct 8, 2012 at 8:38
  • How do you add the tag forms with javascript? Commented Oct 8, 2012 at 8:41

1 Answer 1

1

I got it.....

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

Sign up to request clarification or add additional context in comments.

1 Comment

Great. I suspected it was something simple!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.