Home > Blockchain >  How can I set values for every field in form with Symfony?
How can I set values for every field in form with Symfony?

Time:10-20

I made a search form where it is possible to select 'bigcity' and 'category'. In SearchController.php, I set a session so that when I come back to search page, I retrieve values that I selected. I tried to set these values in {{ form(form) }} written in search.html.twig.

It works properly for 'bigcity' but not for 'category'. Where could be the mistake ?

search.html.twig

{% extends 'base.html.twig' %}

{% block main %}

{{ form_start(form) }}
            <p>Où souhaites-tu faire une sortie ou une activité ?</p>
            {{ form_widget(form.bigcity, {'attr' : {'class' : 'mb-3' }, 'value' : app.session.get('searchFormData').bigcity.id} ) }}
            <p>Quel type de sortie ou d’activité t’intéresse ?</p>
            {{ form_widget(form.category, {'attr' : {'class' : 'mb-3' }, 'value' : app.session.get('searchFormData').category.id} ) }}
            {{ form_widget(form.save, {'label': 'Je valide'} ) }}
        {{ form_end(form) }}

{% endblock %}

** SearchController.php**

<?php

namespace App\Controller\Front;

use App\Form\SearchType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class SearchController extends AbstractController
{
    #[Route('/search', name: 'search')]
    public function search(
        Request $request, 
        SessionInterface $sessionInterface,
        EventRepository $eventRepository,
    ){   
        $searchFormData  = $sessionInterface->get('searchFormData');

        $form = $this->createForm(SearchType::class, ['data' => $searchFormData]);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $data = $form->getData();
            $sessionInterface->set('searchFormData', $data);
            $events = $eventRepository->findAll();
            
            return $this->redirectToRoute("events", [
                'bigcity'=> $form->get('bigcity')->getData()->getId(), 
                'category'=> $form->get('category')->getData()->getId(), 
                'events' => $events
            ]);
        }
        return $this->renderForm('front/search.html.twig', [ 
            'form' => $form,
        ]);
    }
}

SearchType.php

<?php

namespace App\Form;

use App\Entity\BigCity;
use App\Entity\Category;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

class SearchType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('bigcity', EntityType::class, [
                'class' => BigCity::class,
                'choice_label' => 'name',
                'placeholder' => 'Sélectionne une grande ville',
            ])
            ->add('category', EntityType::class, [
                'class' => Category::class,
                'choice_label' => 'image',
                'expanded' => true,
                'multiple' => false,
            ])
            ->add('save', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => null
        ]);
    }
}

CodePudding user response:

That's because in SearchType category has 'expanded' => true, that's why it works with bigcity but not with catgory. What you can do is something like this:

{{ form_label(form.category) }}
{% for key, categoryItem in form.category.children %}
    {{ form_widget(categoryItem, {'attr' : {'class' : 'mb-3' },  'checked': categoryItem.vars.value ==  app.session.get('searchFormData').category.id ? 'checked' : ''  } ) }}
{% endfor %}
  • Related