Search code examples
phpsymfonysymfony4

I can't request array data


This is the buidForm

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('categorie', EntityType::class, [
        // This field shows all the categories
        'placeholder' => 'Choisissez une option',
        'class'    => Categorie::class,
        'mapped' => false,
        'multiple' => true
        ]);
}

And this the controller

$form = $this->createForm(RechercheCandidatType::class, null);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    $categories = $request->request->get('recherche_candidat_categorie');
      foreach ($categories as $categorie) {
            dump($categorie);
        }
}

return $this->render("test.html.twig",['form' => $form->createView()]);

And this the TWIG

        {{ form_start(form,{'attr': {'novalidate': 'novalidate'}}) }}
        <div class="">
            {{ form_errors(form) }}
        </div>

        <div class="row">
            <div class="col">
                {{ form_row(form.categorie) }}
            </div>
        </div>

        {{ form_row(form._token) }}
        <button class="btn btn-success">Afficher</button>
        {{ form_end(form) }} 

![enter image description here This is from Inspect element

 <form name="recherche_candidat" method="post" novalidate="novalidate">
            <div class="">

            </div>

            <div class="row">
                <div class="col">
                    <div class="form-group"><label class="required" for="recherche_candidat_categorie">Categorie</label><select id="recherche_candidat_categorie" name="recherche_candidat[categorie][]" required="required" class="form-control" multiple="multiple"><option value="1">Web</option><option value="2">Mobile</option></select></div>
                </div>
            </div>

            <input type="hidden" id="recherche_candidat__token" name="recherche_candidat[_token]" value="B25v0VqQWQbERl3c3_EsGkyiVZTvC8xn37szufreGmo" />
            <button class="btn btn-success">Afficher</button>
            </form>

The problem is after submitting, i can't receive the array data ($categories = null )

PS : recherche_candidat_categorie = ID of select form


Solution

  • You can't get data using id, you have to use the name, So instead of "recherche_candidat_categorie", use this

    $request->request->get('recherche_candidat');