1

I am currently developing a symfony project for which I need a form to edit an existing entity called Location. One Location object can hold 0 to n Machines. The form should contain multiple checkboxes, with preselected values depending on weather the entity Location already contains any Machine or not. If yes, preselect the checkbox, if not leave it empty.

The Location:

...
class Location
{    
    /**
     * @ORM\OneToMany(targetEntity="Machine", mappedBy="location")
     *  
     */
    private $machines;
...

The Machine:

...
class Machine
{

       /**
     * @ORM\ManyToOne(targetEntity="Location", inversedBy="machines")
     * @ORM\JoinColumn(name="location_id", referencedColumnName="id")
     */
    private $location;  
   ...

I build the form like this:

$form = $this->createFormBuilder($location)
 ->add('name', null , array( 'label' => 'Roomname: ', 'attr'=> array( 'placeholder'=>'Enter a name') ) )
 ->add('descr', null , array( 'label' => 'Description: ',  'attr'=> array( 'placeholder'=>'Enter a description') ) ) 
 ->add('machines', 'entity', array('class' => 'PRwissHostsBundle:Machine',
   'property' => 'name',
   'required' => false, 
   'expanded' => true,
   'multiple' => true, 

   ))
 ->getForm();

The code above results in a form with a couple of checkboxes with the entities name next to it. I want to pre select those checkboxes where the machines already are related to the location.

I have had a look at this question, but unfortunatly the "data" => x only refers to one value.

Any ideas how I can accomplish this? Thanks in advance!

1 Answer 1

1

Add machines to Location entity in the controller, just before you initialize the form. Symfony form will automatically set values for checkboxes based on entity data.

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

Comments

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.