I am creating a form in Symfony in which you must choose a category and a subcategory that depends on the main category (for example, if you select Music the subcategories are pop, rock, etc ... and if it is Shows you should be childish, adults, etc ...
The categories are related in the database without problems. The Subcategories table contains a field in which the parent category of the element is stored.
I have created the form but in the drop-down with the subcategories all appear, when only those that belong to the selected parent category should be shown.
How do I solve it?
This is the form's constructor
//EnquiryType.php
class EnquiryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('categoria');
$builder->add('subcategoria');
$builder->add('espectadores');
$builder->add('fecha');
$builder->add('hora');
$builder->add('tiempo');
$builder->add('presupuesto');
$builder->add('material', 'radio');
$builder->add('materiales');
$builder->add('informacion');
}
And this is the controller
//EventController.php
class EventController extends Controller
{
public function indexAction()
{
$evento = new Evento();
$form = $this->createForm(new EnquiryType(), $evento);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
return $this->redirect($this->generateUrl('CASEventBundle_create'));
}
}
return $this->render('CASEventBundle:Default:form.html.twig', array('form' => $form->createView()));
}
}