Uc.IT_samuel
Uc.IT_samuel

Reputation: 233

How to make checkboxes work in Symfony 2?

I am currently creating a form that lets the user choose a certain skills from a Dropdown and a Checkbox for hobbies that lets the user check as much he/she wants.

Here is my Table for that: CurriculumVitae

/* namespace ........... */

use Doctrine\ORM\Mapping as ORM;

/**
* CurriculumVitae
*
* @ORM\Table(name="foo_cv")
* @ORM\Entity
*/
class CurriculumVitae
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /**
    * @var integer
    * @ORM\ManyToOne(targetEntity="Foo\BarBundle\Entity\Skills")
    * @ORM\JoinColumn(name="skills", referencedColumnName="id")
    */
    private $skills;

    /**
    * @var integer
    * @ORM\ManyToOne(targetEntity="Foo\BarBundle\Entity\Hobby", cascade={"persist"})
    * @ORM\JoinColumn(name="hobbies", referencedColumnName="id")
    */
    private $hobby;

    /*  Setters and Getters   .......  */
}

Here are some codes for my Form Type: CurriculumVitaeType

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('skills', 'entity', array('class' =>'FooBarBundle:Skills','property' => 'skills'))
        ->add('hobby', 'entity', array( 'class' => 'FooBarBundle:Hobby','property' => 'hobbies', 'expanded'=>true,'multiple'=>true, 'label' => 'hobbies'))
        ->add('save','submit',array('label'=>'Submit'))
    ;
}

/* OptionsResolverInterface  .....   */
/* getName()  .... */

I call my form in my twig this way in: cv.twig.html

{{ form(curriculumForm) }} 

And lastly in my controller: CurriculumController

$em = $this->getDoctrine()->getManager();
$cv = new CurriculumVitae();
$curriculumForm = $this->createForm(new CurriculumVitaeType(), $cv);
$curriculumForm->handleRequest($request);

if ($curriculumForm->isValid()) {
    $em->persist($cv);
    $em->flush();
    return $this->redirect($this->generateUrl('foo_main_window'));
}
return array('curriculumForm'=> $curriculumForm->createView());

The Form Displays correctly but when I choose a skill from the dropdown and assign a certain hobby and click on submit, an error is thrown.

Found entity of type Doctrine\Common\Collections\ArrayCollection on association Foo\BarBundle\Entity\CurriculumVitae#hobby, but expecting Foo\BarBundle\Entity\Hobby

I dont know if i missed something but i think the error occurs in the process of persisting the data after the form is submitted.

Upvotes: 1

Views: 76

Answers (1)

Jakub Matczak
Jakub Matczak

Reputation: 15656

That's because you have many-to-one relation, which means

Many CurriculumVitaes can have (the same) single Hobby

But on the other hand you've created in your form a field with option 'multiple'=>true, which means that you let the user to choose multiple hobbies. Therefore form returns ArrayCollection of Hobby entites instead of single instance.

That doesn't match. You need to either remove multiple option, or make many-to-many relation on $hobby property.

Upvotes: 1

Related Questions