Reputation: 5067
Does implementing of AJAX with Symfony2 is so difficult as I see it now?
My goal is to implement a one-page application that displays a form for creating posts (each post has a post title and a post content). When the form is submitted, AJAX takes in charge displaying the post below the form in the div#output.
My template looks like:
{% extends '::base.html.twig' %}
{% block body %}
<form action="{{ path('post_creates', { 'id': entity.id }) }}" id="form_ajax" method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<p>
<input type="submit" value="create" />
</p>
</form>
<ul class="record_actions">
<li>
<a href="{{ path('post') }}">
Back to the list
</a>
</li>
</ul>
<div id="output"></div>
{% endblock %}
{% block javascripts %}
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="http://jwpsrv.com/library/leLjogZ6EeOBvCIACusDuQ.js"></script>
<script src="{{ asset('bundles/testblog/js/test2.js') }}"></script>
<script>
$("#form_ajax").submit(function(){
tinyMCE.get("test_bundle_blogbundle_posttype_postContent").save();
var content= $("#test_bundle_blogbundle_posttype_postContent").val();
var title = $("#test_bundle_blogbundle_posttype_postTitle").val();
$.ajax({
type: "POST",
url: "{{ path('post_creates', { 'id': entity.id }) }}",
data: {datas:title, date:content},
cache: false,
success: function(data){
$('#output').html(data);
}
});
return false;
});
</script>
{% endblock %}
The controller is:
public function createsAction(Request $request, $id)
{
$request = $this->container->get('request');
$entity = new Post();
if($request->isXmlHttpRequest())
{
$title = $request->request->get('datas');
$content = $request->request->get('date');
$em = $this->container->get('doctrine')->getEntityManager();
if($title != '')
{
$entity->setPostContent($content);
$entity->setPostTitle($title);
$id=$entity->getId();
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
$post = $em->getRepository('TESTBlogBundle:Post')->find($id);
}
else {
$post = $em->getRepository('TESTBlogBundle:Post')->find($id);
}
$deleteForm = $this->createDeleteForm($id);
return $this->container->get('templating')->renderResponse('TESTBlogBundle:Post:show.html.twig', array(
'entity' => $post, 'id' => $id,
'delete_form' => $deleteForm->createView(),
));
}
else {
return $this->indexAction();
}
}
I have a the following problem:
Controller requires that you provide a value for the "$id" argument (because there is no default value or because there is a non optional argument after this one).
When I var_dump the $id inside my controller, I always have null. My controller works fine if I pass a value to it in function arguments. My knowledge in Symfony2 doesnt allow me to find what I am missing. Your help is really appreciated.
Upvotes: 0
Views: 1962
Reputation: 13891
The answer is in the returned error message.
The url you're calling through Ajax didn't contain any value for the $id
.
As $id
is required according to your action definition. You've to be sure it's always provided.
Update:
When you run the action
that display the template
for the first time (It may be the same action or another one), it's providing a non-valid entity
(which doesn't contain any id
). So, when it builds the url
you're using in you ajax call
, it calls an url that doesn't contain any id
.
Edit by OP:
The parameter $id
of the $entity
is not defined till you persist data into database. So remove it from the arguments of the controller, and remove it also from the template by removing the entity.id
Upvotes: 1