Home > other >  Symfony. How to create a form car search with tables one to many?
Symfony. How to create a form car search with tables one to many?

Time:11-03

I want the user to select the car type listed in the "car_types" table first. After selecting the type (car / truck), the selection of the vehicle brand associated with the selected vehicle type will be unlocked.After selecting the car brand, the model selection with relation to the brand will be unlocked. And so on...

This is my table schema:

This is my code search form :

class SearchCarsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('typ',EntityType::class,[
                'class' => CarTypes::class,
                'choice_label' => 'name'
            ])
            ->add('mark',EntityType::class,[
                'class' => Brand::class,
                'choice_label' => 'name'
            ])
            ->add('model',EntityType::class,[
                'class' => Models::class,
                'choice_label' => 'name'
            ])
            ->add('generation',EntityType::class,[
                'class' => Generations::class,
                'choice_label' => 'name'
            ])
            ->add('car_body',EntityType::class,[
                'class' => CarBodys::class,
                'choice_label' => 'name'
            ])
            ->add('engine',EntityType::class,[
                'class' => Engines::class,
                'choice_label' => 'name'
            ])
            ->add('equipment',EntityType::class,[
                'class' => Equipment::class,
                'choice_label' => 'name'
            ])

            ->add('Submit',SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            // Configure your form options here
        ]);
    }
}

CodePudding user response:

I had same issue some time ago and the solution that I could find was to create select inputs that feed by ajax requests. Ex: User select car type / truck than you make a ajax request to your controller and return brands for type truck to your twig template. So your entire page is not reloaded but only a part of it is reloaded and get the brands. And so on you feed your select input with the brands using javascript

  • Related