Récuperation données par get

Par Frédouze, il y a 5 ans


Bonsoir à tous, je voudrait faire une condition avec un champs get, mais je n'arrive pas a recupere le nom de mon champ. Je voudrair recupere le nom de l'un des 2 champs avec $nom = $request->request->get('nom'); et l'enregistrer avec la ligne $libelle->setAuteur($nom); dans ma base de donnée. Mais apparament le nom du champs n'est pas bon. J'ai esseyer avec libelle_form[nom] mais ca pas l'aire bon aussi.

Voici mon controleur

<?php // src/Controller/FormController.php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use App\Form\LibelleFormType; use App\Entity\Libelle; class LibelleController extends AbstractController { /** * @Route("/libelle") */ public function new(Request $request) { $libelle = new Libelle(); $libelle->setNom('Hello World'); $libelle->setDate('Un très court libelle.'); $form = $this->createForm(LibelleFormType::class, $libelle); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $nom = $request->request->get('nom'); if( $libelle->getNom() == null) { $libelle->setValidation('vide'); } $libelle->setAuteur($nom); $em->persist($libelle); $em->flush(); } return $this->render('default/new.html.twig', array( 'form' => $form->createView(), )); } }

Voici ma vue

<html> <head></head> <body> {{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }} {{ form_row(form.nom) }} {{ form_row(form.date) }} <button type="submit" class="btn btn-primary">Créer</button> {{ form_end(form) }} </body> </html>

et voici le code source du navigateur

<form name="libelle_form" method="post" novalidate="novalidate"> <div><label for="libelle_form_nom">Nom</label><input type="text" id="libelle_form_nom" name="libelle_form[nom]" maxlength="255" value="Hello World" /></div> <div><label for="libelle_form_date">Date</label><input type="text" id="libelle_form_date" name="libelle_form[date]" maxlength="255" value="Un très court libelle." /></div> <button type="submit" class="btn btn-primary">Créer</button> <input type="hidden" id="libelle_form__token" name="libelle_form[_token]" value="8rKovdlj15umj-dWiUhbW0N5kZhRCTVIh60o1ljUyrw" /></form>

Cordialement

1 réponse

bidule, il y a 5 ans

salut,

C'est le même principe que pour ta condition ;) (si nom est bien l'un des champs de ton formulaire)

$form = $this->createForm(LibelleFormType::class, $libelle); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $nom = $request->request->get('nom'); if( $libelle->getNom() == null) { $libelle->setValidation('vide'); } $libelle->setAuteur($libelle->getNom()); $em->persist($libelle); $em->flush(); }