Reputation: 413
I want to implement a form to create a blog in my website. In the controller I have two functions for this purpose, the first one to render the form and the second one to process the data and set it. The form is displayed however I cannot set it ti redirect to the second function to actually process it and add the data to the DB. I do not know if the problem I have is within the controller or the templates. Can you help me?
I have defined the submit button within the formtype.
Here is the controller:
/**
* Display a form for a new Post
*
* @return array
*
* @Route("/new/_post", name="_blog_backend_post_new")
* @Template("BlogBundle:Backend/Post:new.html.twig")
*/
public function newPostAction()
{
$post = new Post();
$post -> setAuthor($this->getUserManager()->getloggedUser());
$form_post = $this->createForm(new PostType(), $post);
return array(
'form_post' => $form_post->createView(),
);
}
/**
* Creates a new Post entity.
*
* @param Request $request
*
* @Route("/create", name="_blog_backend_post_create")
* @Method("POST")
* @Template("BlogBundle:Backend/Post:new.html.twig")
*
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse
*/
public function createAction(Request $request)
{
$post = new Post();
$post -> setAuthor($this->getUserManager()->getloggedUser());
$form_post = $this->createForm(new PostType(), $post);
$this->$form_post->submit($this->$request);
if ($this->$form_post->isValid())
{
$em = $this->getDoctrine()->getManager();
$em->persist($post);
$em->flush();
return $this->redirect($this->generateUrl('blog_blog_post_show', array('slug' => $slug_post = $post->getSlug()) ));
}
return $this->render('Blog:Post:show.html.twig', array('form_post' => $form_post->createView(),'slug' => $slug_post = $post->getSlug() ));
}
Here are the templates:
{% extends "::base.html.twig" %}
{% block body %}
<h2>Post a Post</h2>
<form>
{% include 'BlogBundle:Backend/Post:edit.form.html.twig' with {'form_post': form_post, route: '_blog_backend_post_create' } %}
</form>
{% endblock %}
--- BlogBundle:Backend/Post:edit.form.html.twig:
{% if form_errors(form_post) %}
<div class="alert alert-block alert-error fade in form-errors">
{{ form_errors(form_post) }}
</div>
{% endif %}
<form class="well" method="post" {{ form_enctype(form_post) }}>
<p>
<label for="">Title</label>
{% if form_errors(form_post.title) %}
<div class="alert alert-block alert-error fade in form-errors">
{{ form_errors(form_post.title) }}
</div>
{% endif %}
{{ form_widget(form_post.title, { 'attr': {'class': 'w100'} }) }}
</p>
<p>
<label for="">Body</label>
{% if form_errors(form_post.body) %}
<div class="alert alert-block alert-error fade in form-errors">
{{ form_errors(form_post.body) }}
</div>
{% endif %}
{{ form_widget(form_post.body, { 'attr': {'class': 'w100'} }) }}
</p>
<p>
<button class="btn" type="submit">Save</button>
</p>
{{ form_rest(form_post) }}
</form>
Corrected Controller code:
$form_post = $this->createForm(new PostType(), $post);
$form_post->handleRequest($request);
if ($form_post->isValid())
Removed double tags from BlogBundle:Backend/Post:edit.form.html.twig
Added action to the form:
<form class="well" action="{{path("_blog_backend_post_create")}}" method="post" {{ form_enctype(form_post) }}>
Upvotes: 0
Views: 1214
Reputation: 1152
You have to make sure you post (not "redirect") the form to the second controller. To do so you need to specify the action attribute of the form-tag.
<form class="well" action="{{path("_blog_backend_post_create")}}" method="post" {{ form_enctype(form_post) }}>
When you submit the from, the code in you createAction will be executed.
(Also, remove the extra form tags in BlogBundle:Backend/Post:new.html.twig)
More preferable way is to use the form()
twig function. So in your twig file, you write:
{% extends "::base.html.twig" %}
{% block body %}
<h2>Post a Post</h2>
{{ form(form_post) }}
{% endblock %}
When you create your form you will aslo need to add the action like:
$form_post = $this->createForm(new PostType(), $post, array(
'action'=>$this->generateUrl('_blog_backend_post_create'),
));
Upvotes: 2