Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can return a response like usual and decide on the view if you want to do a partial render or render the whole page by extending different twig layouts.</p> <p>In your controller:</p> <pre><code>if ($form-&gt;isValid()) { // perform some action, such as saving the task to the database $this-&gt;get('session')-&gt;setFlash('notice', 'Success!'); return $this-&gt;render('success.html.twig'); } else { $this-&gt;get('session')-&gt;setFlash('notice', 'Please correct the errors below'); } </code></pre> <p>And in your views:</p> <pre><code>{# new.html.twig #} {% extends app.request.isXmlHttpRequest ? 'ajax.html.twig' : 'layout.html.twig' %} {% block content %} {% if app.session.hasFlash('notice') %} &lt;div class="flash-notice"&gt;{{ app.session.flash('notice') }}&lt;/div&gt; {% endif %} &lt;form ...&gt; {{ form_widget('form') }} &lt;input type="submit" /&gt; &lt;/form&gt; {% endblock %} {# success.html.twig #} {% extends app.request.isXmlHttpRequest ? 'ajax.html.twig' : 'layout.html.twig' %} {% block content %} {% if app.session.hasFlash('notice') %} &lt;div class="flash-notice"&gt;{{ app.session.flash('notice') }}&lt;/div&gt; {% endif %} {% endblock %} {# ajax.html.twig #} {% block content %}{% endblock %} {# layout.html.twig #} &lt;html&gt; &lt;body&gt; normal page content &lt;div id='content'&gt; {% block content %}{% endblock %} &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>(You might want to put the flash messages part in a macro or an include...)</p> <p>Now simply render the return from the ajax request into the tag you want to place it:</p> <pre><code>$.ajax({ type: "POST", url: "tasks/new", data: {task: foo, dueDate: bar}, }).done(function( msg ) { $('#content').html(msg); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload