Reputation: 97
I have two forms that i want to handle in the same action, actually the first one works pretty good, but the other form doesn't work. This is my action code :
/**
* @Route("/webmaster/gestProf/{idprof}", defaults={"idprof": 0},name="gestProf")
* @Template()
*/
public function gestProfAction(Request $request)
{
$session = new Session();
$session->start();
$em=$this
->getDoctrine()
->getManager();
$repository = $em->getRepository("CNAMCMSBundle:profil");
$profils = $repository->findAll();
foreach ($profils as $prof) {
$id = $prof->getId();
$libelle = $prof->getLibelle();
}
$profil = new profil();
$form = $this->createFormBuilder($profil, array('csrf_protection' => false))
->add('id', 'text', array('attr' => array('maxlength' => '255', 'placeholder' => 'Nouvel Identificateur', 'id' => 'id_prof')))
->add('libelle', 'text', array('attr' => array('maxlength' => '20', 'placeholder' => 'Nouveau Libellé', 'id' => 'libelle')))
->add('idprof', 'hidden', array('mapped' => false,'attr' => array('maxlength' => '20', 'placeholder' => 'Nouveau Libellé', 'id' => 'libelle')))
->add('Edit', 'submit', array('attr' => array('class' => 'btn btn-primary btn-block rounded_btn', 'id' => 'login_btn',
'style' => "width:6vw;height:5vh;padding:0px 0px; position:relative;left:5vmin;top:1vmin;font-size:2vmin;")))
->getForm();
$profile = new profil();
$form2 = $this->createFormBuilder($profile, array('csrf_protection' => false))
->add('id', 'text', array('attr' => array('maxlength' => '255', 'placeholder' => 'Nouvel Identificateur', 'id' => 'id_prof')))
->add('libelle', 'text', array('attr' => array('maxlength' => '20', 'placeholder' => 'Nouveau Libellé', 'id' => 'libelle')))
->add('Ajouter', 'submit', array('attr' => array('class' => 'btn btn-primary btn-block rounded_btn', 'id' => 'login_btn',
'style' => "width:6vw;height:5vh;padding:0px 0px; position:relative;left:5vmin;top:1vmin;font-size:2vmin;")))
->getForm();
if ($request->request->has('form')) {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$idprof = $form->get('idprof')->getData();
$id = $data->getId();
$libelle = $data->getLibelle();
$em = $this
->getDoctrine()
->getManager();
$repository = $em->getRepository("CNAMCMSBundle:profil");
$profil = $repository->find($idprof);
if ($id !== Null) {
$profil->setId($id);
$em->merge($profil);
$em->flush();
}
if ($libelle !== Null) {
$profil->setLibelle($libelle);
$em->merge($profil);
$em->flush();
}
}
}
if ($request->request->has('form2')) {
$form2->handleRequest($request);
// $id2 = $request->request->get('id');
// $libelle2 = $request->request->get('libelle');
if ($form2->isSubmitted() && $form2->isValid()) {
$data2 = $form2->getData();
//$idprof = $form->get('idprof')->getData();
$id2 = $data2->getId();
$libelle2 = $data2->getLibelle();
$em=$this
->getDoctrine()
->getManager();
$profile->setId($id2);
$profile->setLibelle($libelle2);
$em->persist($profile);
$em->flush();
}
}
return $this->render('CNAMCMSBundle:Default:gestProf.html.twig', array('profils'=>$profils,
'form'=>$form->createView(),
'form2'=>$form2->createView()
));
}
And this is their implementation in Twig :
{{ form_start(form, { attr: {novalidate: 'novalidate'} }) }}
<section class="col-lg-9 col-md-9 col-sm-9 col-xs-9" style="position: relative; left: 5vmin;top: 6vmin">
<label style="display:inline-table;">
<span>{{ form_widget(form.id) }}</span>
</label>
</section>
<section class="col-lg-9 col-md-9 col-sm-9 col-xs-9"style="position: relative; left: 5vmin;top: 6vmin">
<label style="display:inline-table;">
<span>{{ form_widget(form.libelle) }}</span>
</label>
</section>
<section class="col-lg-9 col-md-9 col-sm-9 col-xs-9" style="position: relative; left: 5vmin;top: 6vmin">
<label style="display:inline-table;">
<span>{{form_widget(form.idprof, {attr: { value : profil.id}} )}}</span>
</label>
</section>
<section class="col-lg-5 col-md-5 col-sm-5 col-xs-5" style="position: relative; left: 5vmin;top: 6vmin">
<span>{{ form_widget(form.Edit) }}</span>
</section>
{{ form_end(form) }}
{{ form_start(form2, { attr: {novalidate: 'novalidate'} }) }}
<section class="col-lg-9 col-md-9 col-sm-9 col-xs-9" style="position: relative; left: 5vmin;top: 6vmin">
<label style="display:inline-table;">
<span>{{ form_widget(form2.id) }}</span>
</label>
</section>
<section class="col-lg-9 col-md-9 col-sm-9 col-xs-9"style="position: relative; left: 5vmin;top: 6vmin">
<label style="display:inline-table;">
<span>{{ form_widget(form2.libelle) }}</span>
</label>
</section>
<section class="col-lg-5 col-md-5 col-sm-5 col-xs-5" style="position: relative; left: 5vmin;top: 6vmin">
<span>{{ form_widget(form2.Ajouter) }}</span>
</section>
{{ form_end(form2) }}
Upvotes: 1
Views: 169
Reputation: 97
I solved my problem by adding the method post to my second form :
$form2 = $this->createFormBuilder($profile,array('csrf_protection' => false))
->setMethod("POST");
Upvotes: 0
Reputation: 825
First of all I'd recommend removing form classes from the controller and putting them in a separate classes under Form folder of your bundle. Then in this classes give them different prefixes to this forms.
public function getBlockPrefix()
{
return 'form1';
}
Then, name all the form fields correctly and you won't have to apply data to each entity field manually. Also remove this line for both forms.
$request-request->has('form')
Why are you using $em->merge instead of persist while submitting form. Or maybe I'm missing something? Try reading this documentation. http://symfony.com/doc/current/forms.html Symfony has really great documentation.
Upvotes: 1