I'm currently using Symfony 2.0.13 for a project and I'm working on the User registration form. My project uses Doctrine ORM, Twig and the Form component for this.
Every user has to select an Area. The Area object is basically defined like this:
<?php
namespace Acme\AwesomeBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table
* @ORM\Entity
*/
class Area
{
/**
* @var integer $id
*
* @ORM\Column(type="smallint", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @ORM\Column(type="string", length="64", nullable=false)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity="Area", inversedBy="children")
* @ORM\JoinColumn(name="parent")
*/
private $parent;
/**
*
* @ORM\OneToMany(targetEntity="Area", mappedBy="parent")
*/
private $children;
/**
*
* @ORM\Column(type="boolean", nullable=false)
*/
private $selectable;
// + all the default getters and setters generated using the Symfony console
// + a __toString() method which returns the name
As you can see a Area can have sub areas (children) and they can also have sub areas (for example a country would be an area and would have several provinces/states and they would all have cities). Also, an Area can be marked as selectable or not.
I want to show the user a dropdown-box to select one of the areas, but the hierarchy should be clearly visible (I was thinking of indenting the items using hyphens) and Areas marked as not-selectable should be disabled, but shown in the list.
I figure I should create a new Form type, but it's not really clear to me how I should generate the ChoiceList (or choices array). So hopefully someone can be point me in the right direction or has some useful sources for me.
You could use a simple choice
type that is built using the choices
option:
$choices = $this->getFlatChoices();
// in a form type
$builder->add('area', 'choice', array('choices' => $choices));
I would recommend you to look at here to have an example how to do this.