Note that there are some explanatory texts on larger screens.

plurals
  1. POBest way to make a Symfony2 Form with multi select in cascade
    primarykey
    data
    text
    <p>I have 3 entity (Country, Region, City)</p> <pre><code>namespace ****\****Bundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; class Country { private $id; private $name; /** * @var integer $regions * * @ORM\OneToMany(targetEntity="Region", mappedBy="Country") */ protected $regions; //... } class Region { private $id; private $name; /** * @var integer $country * * @Assert\Type(type="****\***Bundle\Entity\Country") * @ORM\ManyToOne(targetEntity="Country", inversedBy="regions") * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false) */ private $country; /** * @ORM\OneToMany(targetEntity="City", mappedBy="Region") */ protected $cities; } class City { private $id; private $name; /** * @var integer $region * * @Assert\Type(type="****\****Bundle\Entity\Region") * @ORM\ManyToOne(targetEntity="Region", inversedBy="cities") * @ORM\JoinColumn(name="region_id", referencedColumnName="id", nullable=false) */ private $region; /** * @ORM\OneToMany(targetEntity="Company", mappedBy="City") */ protected $companys; //... } </code></pre> <p>Here is my Form Class for City:</p> <pre><code>namespace ****\****Bundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class CityType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder -&gt;add('name') -&gt;add('region'); } public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver-&gt;setDefaults(array( 'data_class' =&gt; '****\****Bundle\Entity\City', )); } public function getName() { return 'city'; } } </code></pre> <p>This make a basic HTML5 Form with a textBox for the name and a SelectBox where all the region is available.</p> <p>My question is what is the best way for adding a first SelectBox that will allow me to select the country in order to filter the second SelectBox and decrease the number of choice of Region?</p> <p>EventListener ? The Event Dispatcher Component ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload