Home > database >  i want to use this id route to insert into my value form @Route("/offrerecla/recl/{id}"
i want to use this id route to insert into my value form @Route("/offrerecla/recl/{id}"

Time:04-24

/**
 * @Route("/offrerecla/recl/{id}", name="reclamation_new", methods={"GET", "POST"})
 */
public function nerjj(Request $request, ReclamationRepository $reclamationRepository): Response
{

    $reclamation = new Reclamation();

    $form = $this->createForm(ReclamationType::class, $reclamation);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
    $reclamationRepository->add($reclamation);
        return $this->redirectToRoute('reclamations_moi', [], Response::HTTP_SEE_OTHER);
    }

    return $this->render('home/reclamer_off.html.twig', [
        'reclamation' => $reclamation,
        'form' => $form->createView(),

    ]);

enter image description here

CodePudding user response:

If I understand correctly, you want to show the id from the url in the offreareclamer field of your form. Your form is bound to the Reclamation class, and the offreareclamer field is bound to the Offre class.

If you fetch the Offre entity with the id from the url, and set that onto the empty Reclamation class that you're passing into the form, it should show the correct id in the form:

/**
 * @Route("/offrerecla/recl/{id}", name="reclamation_new", methods={"GET", "POST"})
 */
public function nerjj(Request $request, ReclamationRepository $reclamationRepository, Offre $offre): Response
{

    $reclamation = new Reclamation();
    $reclamation->setOffre($offre);

    $form = $this->createForm(ReclamationType::class, $reclamation);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
    $reclamationRepository->add($reclamation);
        return $this->redirectToRoute('reclamations_moi', [], Response::HTTP_SEE_OTHER);
    }

    return $this->render('home/reclamer_off.html.twig', [
        'reclamation' => $reclamation,
        'form' => $form->createView(),

    ]);

The {id} to Offre entity conversion is done by the SensioFrameworkExtraBundle, you may need to install that if you haven't got it yet: https://symfony.com/bundles/SensioFrameworkExtraBundle/current/index.html

Or you could fetch the Offre entity manually:

/**
 * @Route("/offrerecla/recl/{id}", name="reclamation_new", methods={"GET", "POST"})
 */
public function nerjj(Request $request, ReclamationRepository $reclamationRepository, OffreRepository $offreRepository, $id): Response
{
    $offre = $offreRepository->findOneBy(['id' => $id]);

    $reclamation = new Reclamation();
    $reclamation->setOffre($offre);

    $form = $this->createForm(ReclamationType::class, $reclamation);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
    $reclamationRepository->add($reclamation);
        return $this->redirectToRoute('reclamations_moi', [], Response::HTTP_SEE_OTHER);
    }

    return $this->render('home/reclamer_off.html.twig', [
        'reclamation' => $reclamation,
        'form' => $form->createView(),

CodePudding user response:

 <?php

namespace App\Form;

use App\Entity\Offre;
use App\Entity\Reclamation;
use App\Entity\User;
use DateTime;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;


class ReclamationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): 
void
{
    $builder
        ->add('typereclamation')
        ->add('datereclamation',DateType::class, array(
            'widget' => 'single_text',
            'format' => 'yyyy-MM-dd',
            'data' => new \DateTime("today")
        ))
        ->add('descriptionrecla',TextareaType::class)
        ->add('comuniquer',TextareaType::class)
        ->add('etat',ChoiceType::class, [
            'choices'  => [
                'Rien' => 'Rien',
                'En cours' => 'En cours',
                'Traiter' => 'Traiter',
                'Non Traiter' => 'Non Traiter',
            ],
        ])
        ->add('cinreclameur',EntityType::class, [
            'class' => User::class,
            'choice_label' => 'cin',
        ])

        ->add('offreareclamer',EntityType::class, [
            'class' => Offre::class,
            'choice_label' => 'idoffre',
        ])

    ;
  }

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

CodePudding user response:

/**
  * @Route("/offrerecla/recl/{id}", name="reclamation_new", methods={"GET", 
"POST"})
 */
public function nerjj(Request $request, ReclamationRepository 
$reclamationRepository, OffreRepository $offreRepository, $id): Response
{

    $offre = $offreRepository->findOneBy(['idoffre' => $id]);

    $reclamation = new Reclamation();
    $reclamation->setOffreareclamer($offre);
    $form = $this->createForm(ReclamationType::class, $reclamation);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
    $reclamationRepository->add($reclamation);
        return $this->redirectToRoute('reclamations_moi', [], 
  Response::HTTP_SEE_OTHER);
    }

    return $this->render('home/reclamer_off.html.twig', [
        'reclamation' => $reclamation,
        'form' => $form->createView(),

    ]);
   }
  • Related