Symfony2 渲染一个表单并重定向它来处理它
Symfony2 Render a form and redirect it to process it
我想实现一个表单来在我的网站上创建博客。在控制器中,我有两个功能用于此目的,第一个用于呈现表单,第二个用于处理数据并设置它。显示了表单,但是我无法将其设置为重定向到第二个函数以实际处理它并将数据添加到数据库。我不知道我的问题是在控制器中还是在模板中。你能帮帮我吗?
我已经在表单类型中定义了提交按钮。
这是控制器:
/**
* 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() ));
}
模板如下:
{% 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>
已解决
更正控制器代码:
$form_post = $this->createForm(new PostType(), $post);
$form_post->handleRequest($request);
if ($form_post->isValid())
从 BlogBundle 中删除了双重标签:Backend/Post:edit.form.html.twig
向表单添加操作:
<form class="well" action="{{path("_blog_backend_post_create")}}" method="post" {{ form_enctype(form_post) }}>
您必须确保 post(而不是 "redirect")第二个控制器的表单。为此,您需要指定表单标签的 action 属性。
<form class="well" action="{{path("_blog_backend_post_create")}}" method="post" {{ form_enctype(form_post) }}>
当您提交 from 时,您的 createAction 中的代码将被执行。
(此外,删除 BlogBundle 中的额外表单标签:Backend/Post:new.html.twig)
更好的方法是使用form()
twig 函数。所以在你的树枝文件中,你写:
{% extends "::base.html.twig" %}
{% block body %}
<h2>Post a Post</h2>
{{ form(form_post) }}
{% endblock %}
创建表单时,您还需要添加以下操作:
$form_post = $this->createForm(new PostType(), $post, array(
'action'=>$this->generateUrl('_blog_backend_post_create'),
));
我想实现一个表单来在我的网站上创建博客。在控制器中,我有两个功能用于此目的,第一个用于呈现表单,第二个用于处理数据并设置它。显示了表单,但是我无法将其设置为重定向到第二个函数以实际处理它并将数据添加到数据库。我不知道我的问题是在控制器中还是在模板中。你能帮帮我吗?
我已经在表单类型中定义了提交按钮。
这是控制器:
/**
* 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() ));
}
模板如下:
{% 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>
已解决
更正控制器代码:
$form_post = $this->createForm(new PostType(), $post);
$form_post->handleRequest($request);
if ($form_post->isValid())
从 BlogBundle 中删除了双重标签:Backend/Post:edit.form.html.twig
向表单添加操作:
<form class="well" action="{{path("_blog_backend_post_create")}}" method="post" {{ form_enctype(form_post) }}>
您必须确保 post(而不是 "redirect")第二个控制器的表单。为此,您需要指定表单标签的 action 属性。
<form class="well" action="{{path("_blog_backend_post_create")}}" method="post" {{ form_enctype(form_post) }}>
当您提交 from 时,您的 createAction 中的代码将被执行。
(此外,删除 BlogBundle 中的额外表单标签:Backend/Post:new.html.twig)
更好的方法是使用form()
twig 函数。所以在你的树枝文件中,你写:
{% extends "::base.html.twig" %}
{% block body %}
<h2>Post a Post</h2>
{{ form(form_post) }}
{% endblock %}
创建表单时,您还需要添加以下操作:
$form_post = $this->createForm(new PostType(), $post, array(
'action'=>$this->generateUrl('_blog_backend_post_create'),
));