1

I have a liste of customers, for each customer I have a button to show the edit form of customer. I want when I click into this button, a function ajax show the edit form of the specific customer clicked.

I don't know how can do to render a form into the action (controller) executed with AJAX.

Here the way I create the form in my controller:

public function editAction($id)
    {
        if ($this->container->get('request')->isXmlHttpRequest()) {
            $em = $this->getDoctrine()->getManager();

            $entity = $em->getRepository('ApplicationClientBundle:Client')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Client entity.');
            }

            $editForm = $this->createEditForm($entity);

            $answer =$this->container->get('templating')->renderResponse('ApplicationClientBundle:Client:index.html.twig', array(
                'editForm' => $editForm->createView(),
            ));

            $response = new Response(json_encode(array('editForm'=> $answer)));
            $response->headers->set('Content-type', 'application/json; charset=utf-8');
            return $response;
        }



    }

But the response json is empty.

What can I do in the controller and in the index.html.twig to show the form?

EDIT:

In index.html.twig, I have:

{% block body %}
<div class="content-action">
    <div class="col-xs-12 col-sm-6 col-md-6">
        <p><span>Raison Sociale :</span><span id="raison_sociale" onclick="functionClick(this)">raison sociale</span></p>
        <p><span>Adresse :</span><span id="adresse"> Avenue Majida Boulila imm.....</span><p>
        <p><span>M.F. :</span><span id="matriculeFiscale"> 0000000 PES 000</span></p>
        <p><span>C.P. :</span><span id="codePostal"> 3027</span></p>
        <p><span>Ville :</span><span id=""> Sfax<span/></p>
    </div>
    <div class="col-xs-12 col-sm-6 col-md-6">
        <p><span>Tél. :</span> 74 400 202</p>
        <p><span>E-mail :</span><span id="email"> [email protected]</span></p>
        <p><span>Nom du contact :</span><span id="nomContact"> nom</span></p>
        <p><span>Tél. Mobile :</span><span id="telephone"> 23 447454</span></p>
        <p><input type="hidden" id="id" value="2"/> </p>
    </div>
    <div class="clear"></div>
</div><!--content-action-->
{% endblock %}
{% block javascripts %}
<script>
        function functionClick(element){
        var id = $("#id").val();
            var route = '{{ path('client_edit', { 'id': "PLACEHOLDER" }) }}';
            route = route.replace("PLACEHOLDER", id);
             $.ajax({


                    //On lui indique le type d'envoie des informations

                    type: 'POST',

                    //On lui indique le chemin de la fonction

                    url:  route,



                    //On lui donne la valeur du choix qu'on a fait, et id est la variable qui va contenir notre valeur, nous la retrouvons dans notre controller




                    //Enfin nous lui disons de remplir notre formulaire avec le resultat  

                    success: function(response)

                    {

                        element.innerHTML= {{ the_first_fiels_of_the_form }};
                    }

                }

            );

        }

    </script>
 {% endblock %}

I want have the_first_fiels_of_the_form in the element clicked.

I do: the_first_fiels_of_the_form ={{form_widget(editForm.raisonSociale)}} But no result.

3
  • Ok, Lets get a bit into detail: can you provide the template where you do the ajax call (the important bits, not the whole thing) and the template ApplicationClientBundle:Client:index.html.twig Commented Nov 21, 2014 at 10:26
  • Does rendering the template actually return something? Is $answer set? Commented Nov 21, 2014 at 12:01
  • the json response is empty : {"editForm":{"headers":{}}} Commented Nov 21, 2014 at 12:29

3 Answers 3

1

I resolved my problem like this:

In the controller, I do:

public function editAction($id)
{
    if ($this->container->get('request')->isXmlHttpRequest()) {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ApplicationClientBundle:Client')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Client entity.');
        }

        $editForm = $this->createEditForm($entity);

         return $this->container->get('templating')->renderResponse('ApplicationClientBundle:Client:cordonner.html.twig', array(
        'editForm' => $editForm->createView()
        ));
}

In the template "index.html.twig", I do:

{% block body -%}


...
 {% for entity in entities %}
    <tr>
        <td>{{ entity.raisonSociale}} </td>
        <td>{{ entity.login }}</td>
        <td>{{ entity.password }}</td>
        <td>{{ entity.soldeSMS }}</td>
        <td>

        <a class="modifier-client handler" data-id="{{ entity.id }}"><span class="glyphicon glyphicon1">Update</span></a>
        <a href="#historique" class="modifier-client"><span class="glyphicon glyphicon2">Voir Historique</span></a>
        <a href="#ajout" class="modifier-client"><span class="glyphicon glyphicon3">Ajout transaction</span></a>
        <input type="hidden" name="id_client" id="id_client" value=""/>
    </td>
</tr>

{% endfor %}

<div id="cordonner">
    {% include 'ApplicationClientBundle:Client:cordonner.html.twig' %}
</div>

...
{% endblock %}

{% block javascripts %}
$('.handler').click(function() {
            var id = $(this).data('id');
            var route = '{{ path('client_edit', { 'id': "PLACEHOLDER" }) }}';
            route = route.replace("PLACEHOLDER", id);
            $.ajax({


            //On lui indique le type d'envoie des informations

            type: 'POST',

            //On lui indique le chemin de la fonction

            url:  route,  //<==> editAction($id_customer_selected)



            //On lui donne la valeur du choix qu'on a fait, et id est la variable qui va contenir notre valeur, nous la retrouvons dans notre controller




            //Enfin nous lui disons de remplir notre formulaire avec le resultat  

            success: function(response)

            {

                $('#cordonner').html(response);
                $(".client").hide();
                $(".fich-client").show();
                document.location="#cordonner";


            }

        }

    )});
{% endblock %}

and in the "cordonner.html.twig", I do:

{% if editForm is defined %}
    {{ form(editForm)}}
{% end if%}
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use :

use Symfony\Component\HttpFoundation\JsonResponse;

and later:

$response = new JsonResponse();
$response->setData(array('editForm'=> $answer));
return $response;

Comments

0

Why return json in the first place if you just have a rendered template in it?

public function editAction($id)
    {
        if ($this->container->get('request')->isXmlHttpRequest()) {
            $em = $this->getDoctrine()->getManager();

            $entity = $em->getRepository('ApplicationClientBundle:Client')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Client entity.');
            }

            $editForm = $this->createEditForm($entity);


            return $this->render('ApplicationClientBundle:Client:index.html.twig', array(
                'editForm' => $editForm->createView(),
            ));;
        }



    }

This will do the same. It will return a html response which you can easily append in javascript in the view.

1 Comment

I don't understand the question. The second parameter if $this->render is an array of data you provide the template with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.