ben.IT
ben.IT

Reputation: 1620

How to customise form rendering when based on entity field in symfony 2?

I have created a form based on an entity field. This form is composed of checkboxes that allow user to choose an entity (here 'car'). This works fine but I need to customize the rendering to get extra informations. Currently, the only information that is displayed is the 'id' attribute. In my case, I would like to display extra entity informations in the form such as color, brand etc ... Does anyone know how to proceed ?

The controller :

        public function chooserAction() {
        //symfony.com/doc/current/reference/forms/types/entity.html
        $cars = $this->getDoctrine()
                ->getRepository('CarBundle:Car')
                ->find(1);
        $formBuilder = $this->createFormBuilder();
        /*
          foreach ($cars as $car) {
          $formBuilder->add($car->getId() ,'checkbox')->getForm();
          }
         */
        $formBuilder->add('cars', 'entity', array(
            'class' => 'CarBundle:Car',
            'property' => 'id',
            'expanded' => 'true',
            'multiple' => 'true',
        ));

        $formBuilder->add('save', 'submit');
        $form = $formBuilder->getForm();
        $request = $this->get('request');
        $form->handleRequest($request);
        if ($form->isValid()) {
            echo "ok";

//            return $this->redirect($this->generateUrl('car_show', array('id' => $car->getId())));
        }

        return $this->render('CarBundle:Car:chooser.html.twig', array('form' => $form->createView()));
    }

The entity :

<?php

namespace Foobar\CarBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Car
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Foobar\CarBundle\Entity\CarRepository")
 */
class Car
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="brand", type="string", length=255)
     */
    private $brand;

    /**
     * @var string
     *
     * @ORM\Column(name="color", type="string", length=255)
     */
    private $color;

    /**
     * @var integer
     *
     * @ORM\Column(name="power", type="integer")
     */
    private $power;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Car
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set brand
     *
     * @param string $brand
     * @return Car
     */
    public function setBrand($brand)
    {
        $this->brand = $brand;

        return $this;
    }

    /**
     * Get brand
     *
     * @return string 
     */
    public function getBrand()
    {
        return $this->brand;
    }

    /**
     * Set color
     *
     * @param string $color
     * @return Car
     */
    public function setColor($color)
    {
        $this->color = $color;

        return $this;
    }

    /**
     * Get color
     *
     * @return string 
     */
    public function getColor()
    {
        return $this->color;
    }

    /**
     * Set power
     *
     * @param integer $power
     * @return Car
     */
    public function setPower($power)
    {
        $this->power = $power;

        return $this;
    }

    /**
     * Get power
     *
     * @return integer 
     */
    public function getPower()
    {
        return $this->power;
    }
}

The view :

car chooser
{{ form(form) }}

Upvotes: 1

Views: 110

Answers (1)

L&#233;o Benoist
L&#233;o Benoist

Reputation: 2541

You could create a toString() method in your Car entity.

public function __toString()
{
    return '' . $this->getName();
}

If you want more thinks like pictures you could follow that http://symfony.com/doc/current/cookbook/form/form_customization.html

Upvotes: 1

Related Questions