我想使用这个 id 路由插入到我的值表单中 @Route("/offrerecla/recl/{id}"

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

/**
 * @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

 <?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,
    ]);
}
 }

如果我理解正确,您想在表单的 offreareclamer 字段中显示 url 中的 id。 您的表单绑定到 Reclamation class,offreareclamer 字段绑定到 Offre class.

如果您从 url 中获取具有 idOffre 实体,并将其设置到空的 Reclamation class 上传递到表单中,它应该以以下形式显示正确的 ID:

/**
 * @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(),

    ]);

{id}Offre 实体转换由 SensioFrameworkExtraBundle 完成,如果您还没有安装它,则可能需要安装它:https://symfony.com/bundles/SensioFrameworkExtraBundle/current/index.html

或者您可以手动获取 Offre 实体:

/**
 * @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(),
/**
  * @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(),

    ]);
   }