Show specific values in a symfony form?

0

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()));
    }
}
    
asked by Borjeitor 29.11.2016 в 18:55
source

1 answer

2

I hope it's not too late, you should surely work with form events, or do the form without using the form component

<form>
   <select onchange="llamoMiServicioDepende()">
      <option value="1">Value1</option>
   </select>
   <select id="hijo"> /*Recargas por js los valores de este select al cambiar el select de arriba*/
      <option value="1">Value1</option>
   </select>
</form>

If you see that you only need it once, it could be a valid solution (you have to handle the security of your form manually :() Or you could use the solutions on these websites, which are more great: enter the description of the link here enter the description of the link here

    
answered by 16.02.2017 в 19:52