Question

I'm using Symfony 2.3.4 and of course Twig.

Now, I'm trying to set twig's function path() to a form's action, the problem is... well that I can't.

when I run this:

$('#prof_create').prop('action',"{{ path('profesor_create') }}");

what I'm aiming for is this result:

<form id="prof_create" class="form-horizontal sf_admin_form_area" 
action="/SVN/Symfony/web/app_dev.php/admin/profesor/create" method="post" 
{{ form_enctype(form) }}>

being {{ path('profesor_create') }} -> /SVN/Symfony/web/app_dev.php/admin/profesor/create

the javascript file shows no error nor warning but it's really not working because when I try to submit the form: Error

No route found for "POST /admin/profesor/%7B%7B%20path(%27profesor_create%27)%20%7D%7D"

which means it did not "transform"

{{ path('profesor_create') }}

into

/SVN/Symfony/web/app_dev.php/admin/profesor/create

what I want NOT to do is to write down the entire address in the js function, is there a way to get this done?

Thanks in adv.

No correct solution

OTHER TIPS

Twig and its Variables are rendered and processed before the actual HTML output is shown in your browser.

Therefore in that case {{ path('profesor_create') }} is just another string, which is not handled by the Twig Engine since it has run already and generated the output.

Either render your route before hand and set it where you need it, or use a javascript routing bundle like https://github.com/FriendsOfSymfony/FOSJsRoutingBundle to render your route in JS to work with it.

{# ../some.html.twig #}
<form id="prof_create" class="form-horizontal 
sf_admin_form_area" action="{{ path('profesorext_create') }}" 
      method="post" {{ form_enctype(form) }}>
        {{ form_widget(form) }}

    <div class="form-actions">
        <button onclick="before_submit_action()" id="submit_prof"  class="btn btn-primary">
        <i class="icon-ok"></i> {{'Guardar' | trans}}</button>

        <a class="btn" href="{{ path('profesorext') }}">
            <i class="icon-ban-circle"></i> {{'Cancelar' | trans }}</a>
    </div>
</form>

<script type="text/javascript">
function before_submit_action() {
    if(<condition_met>)
        $('#prof_create').attr('action',{{ path('profesor_create') }});
    else
        $('#prof_create').attr('action',{{ path('profesorext_create') }});
}
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top