[英]Symfony2 render and form
我有一个要在网站的多个页面中使用的表格。 所以我在页面中调用类似的方法:
{{ render(controller('ProjectApplicationBundle:Application:form')) }}
我有formAction方法:
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 ...
}
这不是我的代码,这只是一个示例。
所以我添加了一条创建发送表格的路线
project_application_form:
pattern: /form
defaults: { _controller: ProjectApplicationBundle:Application:form }
现在我的问题:)
1)我想使用url阻止对表单的访问,只允许进行数据处理。
2)如果出现错误,页面将不会重定向,因此显示的表单没有布局,因此我想在呈现表单的页面中获取带有错误的表单。
编辑:我也许有第二点的解决方案,但这不是很适当。 在我的渲染路径的参数中设置并返回该路径,而不是渲染渲染表单。 最后,我使用一个会话来放置错误。
我希望这对你来说很清楚,对不起我的英语。
您必须保护处理表单的操作,并仅通过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
语法,它将为:
process_form_register:
pattern: /register/process
defaults: { _controller: ...... }
requirements:
_method: POST
为您的2)点打开一个新问题,因为每个线程只有一个问题,这是另一个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.