hous
hous

Reputation: 2679

Select box related on another select box

I have an entity Travel has an attribute country and I have an entity City related to Travel. I'd like that when I choose a country showing all cities related. In fact I don't know Ajax and I need a help

class TravelType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
      $builder
        ->add('country', 'country', array(
         'required' => true,
         'label' => 'Country',
         ))
        ->add('destination', 'entity',array(
         'required' => true,
         'class' => 'ProjectAdminBundle:City',
         'property' => 'name',
         'multiple' => false,
         'expanded' => false,
         'empty_value' => 'Choose a city',
         'label' => 'Destination',
         ))
         //.....
    }
}

and this is entity Travel:

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

 /**
 * @ORM\Column(name="country", type="string", length=255, nullable=false)
 *
 * @Assert\Country
 */
protected $country;

/**
 * @ORM\ManyToOne(targetEntity="Project\AdminBundle\Entity\City", inversedBy="travels")
 * @ORM\JoinColumn(nullable=false)
 */
protected $destination;
//........

}

Each city has a country code, for exemple:

London -> UK

Paris -> FR .....

Upvotes: 1

Views: 54

Answers (1)

Alexandru Furculita
Alexandru Furculita

Reputation: 1373

A similar case like yours is discussed in details in the Symphony Cookbook:

http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms

Upvotes: 1

Related Questions