简体   繁体   中英

Using the same form multiple times with different id

In my case, i would like to add for each application OneToMany links of documentation with the same form who contain two inputs, one for the name of the documentation and the second is for the link address documentation, the problem is that it work perfectly only for the first application in the list, but if i try to add a name and link address for the second application on the list or even the third.., when I submit my form, Symfony does not save the information in the database (without error message). By the way i'm using modals to open the form. As a beginner and in my first steps with symfony, i tried to read the documentation but i couldn't find a solution to resolve my problem, please find below the related code, thanks!

index.html.twig

{%for applications in applications %}
.....
<div class="tab-pane fade"  id="dropdown-kv-21-1{{applications.id}}">
  <p>Ajout de liens</p>

   {{ form_start(form) }}

      <div class="form_group">
       <label for="{{form.nom.vars.id}}">Nom</label>
          <input type="text" class="form_control" id="{{form.nom.vars.id}}"name={{form.nom.vars.full_name}}" value="{{form.nom.vars.value}}">
   {{form_errors(form.nom)}}
   {% do form.nom.setRendered %}
 </div>

 <div class="form_group">
     <label for="{{form.lien.vars.id}}">Lien</label>
         <input type="text" class="form_control" id="{{form.lien.vars.id}}" name="{{form.lien.vars.full_name}}" value="{{form.lien.vars.value}}">
       {{form_errors(form.lien)}}
       {% do form.lien.setRendered %}
 </div>

 <div class="form_group">
   <select id="{{form.application.vars.id}}" class="form-control" name="{{form.application.vars.full_name}}" >
       <option value="{{applications.id}}">{{applications.id}}</option>
   </select>
 {{form_errors(form.application)}}
 {% do form.application.setRendered %}
 </div>

 <input  type="submit" class="btn btn-success" value="Ajouter" style="transform: translate(27em);"id="carto_cartographiebundle_liendocapp_ajouter"name="carto_cartographiebundle_liendocapp[ajouter]">

 {% do form.ajouter.setRendered %}

 {{form_end(form)}}

{%endfor%}

AcceuilController

public function indexAction(Request $request)
{

    $em = $this->getDoctrine()->getManager();
    $applications = $em->getRepository('CartoBundle:Application')->findAll();
    if($request->isMethod('POST')){
        $nomApp = $request->get('nomApp');
        $applications = $em->getRepository('CartoBundle:Application')->findBy(array("nomApp"=>$nomApp));
    }

    $lienDocApp = new LienDocApp();
    $form=$this->createForm(LienDocAppType::class, $lienDocApp);
    $form->handleRequest($request);
    if ($form->isSubmitted()&& $form->isValid()){
        $em = $this->getDoctrine()->getManager();
        $em->persist($lienDocApp);
        $em->flush();


    }

    return $this->render('CartoBundle:Accueil:index.html.twig', array(
        'applications'=>$applications,
         'lienDocApp' => $lienDocApp,
        'form' => $form->createView(),
    ));
}

LienDocAppType

class LienDocAppType extends AbstractType{

/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('nom')
            ->add('lien')
            ->add('application')
            ->add('ajouter', SubmitType::class);
}/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Carto\cartographieBundle\Entity\LienDocApp'
    ));
}

/**
 * {@inheritdoc}
 */
public function getBlockPrefix()
{
    return 'carto_cartographiebundle_liendocapp';
}


}

i finally found the solution for my problem and i'm posting it here maybe it will be helpful form some one,

actually there is a hidde, input named _token, all i did is changing the Id of this input with the id of the chosen application

 <input type="hidden" id="{{ form._token.vars.id }}" name="{{form._token.vars.full_name}}" value="{{ form._token.vars.value }}">

will be:

  <input type="hidden" id="{{ applications.id }}" name="{{form._token.vars.full_name}}" value="{{ form._token.vars.value }}">

cheers !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM