Error with path in Symfony and Ajax template

0

I am trying to test a form with symfony with a code copied from the internet. It is about two selects, province and city, in which the values of City depend on what is selected in the Province, but when I load the page it marks the following error:

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route" province_ajax_call "as such a route does not exist.") in NoxLogicDemoBundle: Default: index.html.twig at line 38.

The code is as follows:

{% block title %}Contact{% endblock%}

{% block body %}
    <header>
        <h1>Crear evento</h1>
    </header>

    <p>Want to contact symblog?</p>

     <form action="{{ path('NoxLogicDemoBundle_formu') }}" method="post" {{ form_enctype(form) }} class="evento">
        {{ form_errors(form) }}

        {{ form_row(form.name) }}
        {{ form_row(form.province) }}
        {{ form_row(form.city) }}


        {{ form_rest(form) }}

    </form>

    {% block javascripts %}

    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function () 
        {
            $('#account_type_province').change(function()
            {
               var val = $(this).val();
               $.ajax(
               {
                    type: "POST",
                    url: "{{ url('province_ajax_call') }}?province_id=" + val,
                    success: function(data) 
                    {
                        // Remove current options
                        $('#account_type_city').html('');
                        $.each(data, function(k, v) {
                            $('#account_type_city').append('<option value="' + v + '">' + k + '</option>');
                        });
                    }
                });
                return false;
            });
        });
    </script>

    {% endblock %}

{% endblock %}

All the code I am using is copied from a page that explains how to make dependent selects in symfony: link

I understand that the link is not registered in the routing file and I replace it with the route of the form. This way it goes, but the selection of cities is always empty, so there is still a failure.

    
asked by Borjeitor 02.12.2016 в 15:20
source

1 answer

0
$.ajax({
    type: "POST",
    url: "{{ url('province_ajax_call') }}?province_id=" + val,
    success: function(data) 
    {
        // Remove current options
        $('#account_type_city').html('');
        $.each(data, function(k, v) {
            $('#account_type_city').append('<option value="' + v + '">' + k + '</option>');
        });
    }
});
return false;

Reviewing this specific part of the code, if you verify, you are telling the AJAX to pass the information with POST and you are sending GET parameters ( ?province_id=" + val ); to solve this the ajax must be something like this

type: 'post'
data: {'province_id' : val}
url: "{{ path('province_ajax_call') }}

Note that I changed {{url(...)}} by {{path(..)}} since this is the current way to generate the routes; On the other hand, it is recommended that you save the route generator in your routes file routing.yml (forgive the redundancy), I enclose an example in YML

nombre_de_la_ruta:
    path:     /algo/ajax/
    defaults: { _controller: NombreDelBundle:AccionEnControlador}

with this, the path would be, in example mode {{path('nombre_de_la_ruta')}} .

Then, in the controller

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
public function AccionEnControladorAction(){
  $request = Request::createFromGlobals();
  $provinceId = $request->request->get('province_id');
  //acciones que realizarás

  $response = new JsonResponse($retorno);
}

with this you will be returning the data in json format, which is what corresponds to this example, finally in .success(data) you can manipulate the return data.

    
answered by 07.12.2016 / 15:02
source