我无法请求数组数据

时间:2019-03-26 10:16:09

标签: php symfony symfony4

这是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
        ]);
}

这是控制器

$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()]);

这是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 这是来自检查元素

 <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>

问题是提交后,我无法收到数组数据($ categories =

  

PS:recherche_candidat_categorie =选择表格的ID

1 个答案:

答案 0 :(得分:3)

您无法使用ID获取数据,必须使用名称,因此,请使用此名称代替“ recherche_candidat_categorie”

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