Symfony2渲染和形成 [英] Symfony2 render and form
问题描述
我有一个要在网站的多个页面中使用的表格. 因此,我在页面中调用了这样的方法:
I have a form that I'd like to use in several pages of my website. So I call the method like that in my page:
{{ render(controller('ProjectApplicationBundle:Application:form')) }}
我有formAction方法:
And I have my formAction method:
public function formAction()
{
$form = $this->createForm($type,$obj);
if('POST' == $this->getRequest()->getMethod()){
$form->submit($this->getRequest());
if($form->isValid()){
//redirect to a page after the form
}
}
//render the form template ...
}
这不是我的代码,这只是一个示例.
This is not my code, this is just an example.
所以我添加了一条创建发送表单的路线
So I add to create a route to send my form
project_application_form:
pattern: /form
defaults: { _controller: ProjectApplicationBundle:Application:form }
现在是我的问题:)
1)我想使用url阻止对表单的访问,只允许进行数据处理.
1) I'd like to block the access to my form using the url, just allow for the data processing.
2)如果出现错误,页面将不会重定向,因此显示的表单没有布局,因此我想在呈现表单的页面中获取带有错误的表单.
2) in case of error the page is not redirect, so the form is display without layout, so I'd like to get the form with errors in the page that I render the form.
编辑:我也许对第二点有解决方案,但这不是很恰当. 在我的渲染路径的参数中设置并返回到该路径,而不是渲染渲染表单.最后,我使用一个会话来放置错误.
I maybe have a solution for the second point but this is not very proper. set in argument of my render the route and return to the route instead of render render the form. Finally I use a session to put the error.
我希望这对您来说很清楚,对不起我的英语.
I hope this is clear for you, sorry for my english.
推荐答案
您必须保护处理表单的操作,并仅通过POST
You'll have to secure the action where you process your form and make it available only by POST
示例:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class RegisterController extends Controller
{
/**
* @Route("/register/process", name="process_form_register")
* @Method({"POST"})
*/
public function processAction(Request $request)
{
$form = $this->createFrom(new RegisterType());
$form->submit($request);
if($form->isValid()){
//redirect to a page after the form
}
}
}
如果对路由使用yml
语法,则将为:
If you use yml
syntax for the route, it's gonna be :
process_form_register:
pattern: /register/process
defaults: { _controller: ...... }
requirements:
_method: POST
为您的2)点打开一个新问题,因为每个线程只有一个问题,这又是另一个问题
Open a new question for your 2) point because only one question per thread and it's an other problematic
这篇关于Symfony2渲染和形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!