0
votes

I got a form in Symfony but it won't validate and I don't know why.

From UserBundle/Entity/Task.php

/**
 *
 * @ORM\ManyToMany(targetEntity="Project\TaskBundle\Entity\Task", inversedBy="users")
 * @ORM\JoinTable(name="project_users_tasks")
 * 
 */
protected $tasks;

From UserBundle/Form/CreateUserFormType.php which is my formbuilder:

 $builder->add('tasks', 'entity', array(
            'class' => 'TaskBundle:Task',
            'query_builder' => function(EntityRepository $er) {
                return $er->createQueryBuilder('b')
                ->andWhere('b.owner = :owner')
                ->setParameter('owner', $this->securityContext->getToken()->getUser())
                ->addOrderBy('b.updated', 'ASC');
            },
            'expanded' => false,
            'multiple' => false
            ));

The post request in my browser:

------WebKitFormBoundary6IBT2Ycy78N9AI7u
Content-Disposition: form-data; name="createUser[tasks][]"

14

The result I get for the form is an error concerning the tasks: "This value is not valid"

I got no other validation what so ever. So why can't the task 14(which is clean the dishes for example) be assigned to my user? I mean the id of the task should work or not?

Edit: Symfony doesn't seem to recognize the data, that's why. A print_r of $form->getData();

[tasks:protected] => Doctrine\Common\Collections\ArrayCollection Object
        (
            [_elements:Doctrine\Common\Collections\ArrayCollection:private] => Array
                (
                )

        )

But how can that be? I can see that my browser is posting the data.

1
Do you have Branch with id=14 when you submit the form? - Ziumin
Sorry, I had a typo there. It's actually a task but yes, I have a task with id=14 when I submit the form. - Evo_x
hm.. is current user (on form show and on form submit action) an owner of the task with id=14 - Ziumin

1 Answers

0
votes

At first glance, there are two brackets too much in your submitted field name: createUser[tasks][] should be createUser[tasks], since the entity field is a collapsed, single-valued entity field.

Also try to debug Symfony's request object. var_dump($request->request->get('createUser')) should return something like

array(
    'tasks' => 14,
)

If, on the other hand, you really want to submit one or more tasks, set the "multiple" option on the entity field to true. Then the request data should be something like

array(
    'tasks' => array(
        0 => 14,
    )
)