1

I have a problem using forms in Symfony 2 with mongoDB documents.

I'm trying to have a form that will represent my first document (Post) with a relation oneToMany to Tags (reference)

The relation is declared like this :

/**
 * @Assert\Collection
 * @MongoDB\ReferenceMany(targetDocument="Acme\ManagerBundle\Document\Tags")
 */
protected $tags;

A tag has an Id and a Name.

I have tried a lot of things to make it work

$form = $this->createFormBuilder($tag)->add('tags', 'choice', array('choices' => $tags, 'multiple' => true, 'expanded' => true, 'empty_value' => true,  ))

The form show the choices but once its submited the form is not valid and keep showing thhis error :

"The fields "0", "1", "2" were not expected"

I've also tried this : symfony2 form choice and mongodb

But the use of it is kinda confusing

UPDATE

This is what i get after the post is submited :

object(Doctrine\Common\Collections\ArrayCollection)#795 (1) {
  ["_elements":"Doctrine\Common\Collections\ArrayCollection":private]=>
  array(2) {
    [0]=>
    object(Acme\ManagerBundle\Document\Tags)#723 (2) {
      ["id":protected]=>
      string(24) "4f7a0eb1ecd111b99c3d2f25"
      ["name":protected]=>
      string(6) "Fruits"
    }
    [1]=>
    object(Acme\ManagerBundle\Document\Tags)#720 (2) {
      ["id":protected]=>
      string(24) "4f7a0ec7ecd111b99c3d2f26"
      ["name":protected]=>
      string(10) "Vegetables"
    }
  }
}

So now i understand why i have "The fields "0", "1", "2" were not expected" but i dont understand why Symfony doesn't process it.

I've been looking a the possible bundles but nothing

I have no idea how to have a nice form that will hydrate my object and the related objects, does anyone has a solution for this issue or other idea to solve this?

Thanks a bunch !

Community
  • 1
  • 1
Bobby Six
  • 11
  • 3
  • What did you try from that question? The answer says to install DoctrineMongoDBBundle and use 'document' instead of 'entity'. – meze Mar 31 '12 at 22:34
  • I tried "choice" and "collection", DoctrineMongoDBBundle is of course already installed and i have use 'document' instead of 'entity' but still the same problem. – Bobby Six Apr 02 '12 at 19:57

2 Answers2

0

A choice field won't save by default (although you could manually do it on form submission). You need to look into the document type which is admittedly not documented well but it's essentially the same type as entity here.

I didn't see this was from 3 years ago! Well, it's here in case others find this page I guess.

John Pancoast
  • 1,241
  • 16
  • 14
0

Without seeing the data involved I can only make a best guess here.

It feels like your line of code should look something like.

$tags = $post->getTags();

$fixedTags = array();
foreach ($tags as $tag) {
    $fixedTags[$tag->getId()] = $tag->getName();
}

$form = $this->createFormBuilder($post)
    ->add(
        'tags', 
        'choice', 
        array(
            'choices' => $fixedTags,
            'multiple' => true,
            'expanded' => true,
            'empty_value' => true
        )
    );

Now I think whats happening is you are getting your $tags data in a form like this.

array(0 => (Object)Tag, 1 => (Object)Tag, 2 => (Object)Tag)

Where as what you really want is probably like this.

array('topic1' => 'Topic 1', 'topic2' => 'Topic 2', 'topic3' => 'Topic 3')

If you this isn't the case, reply with some data output and I'm sure we'll be able to help some more.

Jamie Sutherland
  • 2,760
  • 18
  • 19
  • Hi Jamie and thank you, i have tried just with this array you gave as a test array('topic1' => 'Topic 1', etc and now the error is : "The fields "0" were not expected" – Bobby Six Apr 02 '12 at 19:57
  • Anybody has an idea ? i'm still stuck with this The fields "0" were not expected And i really don't know how to resolve it ! Thanks – Bobby Six May 08 '12 at 18:51
  • Looking at your update. the data is in the wrong format still. I'll update my post to how I think your data should look to make it work. I'll point out that I don't use symfony so I'm no expert here. I'm just going by what the documentation says here. http://symfony.com/doc/current/reference/forms/types/choice.html – Jamie Sutherland May 09 '12 at 12:16
  • It works with a simple array OR with a OneToOne but not with a OneToMany relationship. whatever type of field i try it's always throwing the same error :(. I have also tried this : http://pookey.co.uk/wordpress/archives/286-creating-a-custom-form-field-type-in-symfony-2 (en) and this : http://www.developpez.net/forums/d1082815/php/bibliotheques-frameworks/symfony2/formulaires-liste-deroulante/ (fr) but no positive result... – Bobby Six May 11 '12 at 19:36
  • I have also tried allowExtraFields = true; and it resolve the problem but do not save anything in the mongodb document. – Bobby Six May 11 '12 at 21:00