Bonjour à tous, j'ai 2 formulaires qui enregistrer chacun sur 2 table différentes et j'aimerai les fusionner afin que l'on puisse remplir les champs et qui puissent enregistrer sur les tables respectives.

Voici mes contrôleur

<?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\SocieteType;
use App\Entity\Societe;

class SocieteController extends AbstractController
{
    /**
 * @Route("/societe")
 */
public function new(Request $request)
{
    $societe = new Societe();

    $societe->setAdresse('adresse');
    $societe->setVille('ville');
    $societe->setCodepostal('code postal');
    $form = $this->createForm(SocieteType::class, $societe);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $formValues = $request->request->get('societe_form');
        $nom = $formValues['nom'] ?? null;

        $em->persist($societe);
        $em->flush();
    }

    return $this->render('default/societe.html.twig', array(
        'form' => $form->createView(),
    ));
}
}
<?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();

        $formValues = $request->request->get('libelle_form');
        $nom = $formValues['nom'] ?? null;

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

<html>
    <head></head>
    <body>
        {{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}

            {{ form_row(form.adresse) }}
            {{ form_row(form.ville) }}
            {{ form_row(form.codepostal) }}
            <button type="submit" class="btn btn-primary">Créer</button>
        {{ form_end(form) }}
    </body>
</html>
<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>

2 réponses


Il faut créer un fichier formtype qui fusionne les deux formulaire