I'am trying to load differents formType following user choice in a select tag from the main form for an AddAction.
Let me explain you this briefly with this image:
As you can see, an equipement have one type. and depending on its type, it can be either of the Stockages type or Generators type (sorry for the typo in my schema, I forgot the 's' for Stockages).
PS: Just know that I have 9 other entities like generators and stockages, I put these two for the example. That's why I need to pass the adding of the specific formType from the choice made in the previous select through Ajax.
Now I have the main form for equipement, EquipementsType.php:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('code')
->add('brand')
->add('typesequipement', 'entity', array(
'empty_value' => '--select a type of equipements--',
'choices' => '--select a type of equipements--',
'class' => 'MySpace\MyBundle\Entity\Typesequipement',
'property' => 'nameType',
'multiple' => false,
'mapped' => true,
'label' => 'choose a type: ',
))
->add('stockages')
->add('generators')
;
}
I call this main form in my controller for the AddAction() and I remove the two fields stockages and generators like this:
public function addAction() {
$em=$this->getDoctrine()->getManager();
$equipements = $em->getRepository('MySpaceMyBundle:Equipements');
$equipements = new Equipements;
$form=$this->createForm(new EquipementsType(), $equipements);
$form->remove('stockages');
$form->remove('generateurs');
$request = $this->getRequest();
if ($request->isMethod('POST') | ($form->isValid())) {
$form->bind($request);
$em->persist($equipements);
$em->flush();
return $this->redirect($this->generateUrl('indexEquipements'));
} else {
return $this->render('MySpaceMyBundle:MyFolder:addEquipements.html.twig', array('form' => $form->createView() ));
}
}
As you can see, in my main form, the field typesequipement is a select, and according to the choice of the user in this select, I have to display either the form of Stockages (StockagesType.php) or the Generators form (GeneratorsType.php) using AJAX.
This is my Ajax method in the same controller:
/**
*
* @Route("/manageequipements/addEquipementsform", name="formAddEquipements")
* @Method("get")
*/
public function formAddEquipementsAction() {
$request = $this->getRequest();
$em = $this->getDoctrine()->getEntityManager();
if($request->isXmlHttpRequest()) {
$id = '--selectionnez un type d\'équipement--';
$id = $request->get('id');
if ($id != '--selectionnez un type d\'équipement--' | $id!='') {
switch ($id) {
//Stockages
case '1':
$type = $em->getRepository('MySpaceMyBundle:Stockages');
$type = new Stockages;
$formTypesEquipement=$this->createForm(new StockagesType(), $type);
break;
//Generators
case '2':
$type = $em->getRepository('MySpaceMyBundle:Generators');
$type = new Generators;
$formTypesEquipement=$this->createForm(new GeneratorsType(), $type);
break;
}
$response = new Response();
$data = json_encode($id); // formater le résultat de la requête en json
$response->headers->set('Content-Type', 'application/json');
$response->setContent($data);
return $response;
}
} else {
return new Response('error');
}
}
For now, nothing's working. Indeed, If I choose a value in my select typesequipement, nothing's happened. Of course I am i am aware that my ajax method is not finished, but I am a little lost.
In my twig I have this script for the form, I used the onChange attribute for the field typesequipement named formTypesEquipement:
function forTypesEquipement(){
var id_select = $('#mySpace_myBundle_equipements_typesequipement').val();
$.ajax({
url: "{{ path('formAddEquipements') }}",
type: 'GET',
data: {'id': id_select},
dataType: 'json',
success: function(json){ // quand la réponse de la requete arrive
$('#mySpace_myBundle_equipements_typesequipement').html('');
}
});
}
Someone know how to load a form or another for this case using Ajax, i-e display with Ajax the GeneratorsTypes.php if I choose generators in my typesequipement select, or display StockagesTypes.php if I choose the value stockages in my typesequipements select ?
Thank You in advance.
Aucun commentaire:
Enregistrer un commentaire