Symfony提交到相同的URL [英] Symfony submit to same url
问题描述
我有一个带有一些文本字段的表单,并且有一个预览按钮,需要将表单提交给同一控制器.然后在控制器中,我需要提取这些值,并使用这些值填充表单以供模板查看.实现此目标的最佳方法是什么?我是新手,请注意.
I have a form with some text fields and I have a preview button that needs to submit the form to the same controller. And then in the controller, I need to extract the values and populate a form with these values for the template to see. What is the best way to achieve this? I'm a newbe so please be clear.
推荐答案
示例控制器:
public function myControllerName(sfWebRequest $request)
{
$this->form = new myFormClass();
}
在模板中使用<?php echo $form->renderFormTag( url_for('@yourRoutingName'), array('method' => 'POST') ); ?>
,然后将@yourRoutingName
更改为指向控制器的那个.
Use <?php echo $form->renderFormTag( url_for('@yourRoutingName'), array('method' => 'POST') ); ?>
in your template and change @yourRoutingName
to the one pointing to your controller.
现在将您的控制器更改为以下内容:
Now change your controller to be something like this:
public function myControllerName(sfWebRequest $request)
{
$this->form = new myFormClass();
if ($request->isMethod(sfRequest::POST)
{
$this->form->bind( $request->getParameter( $this->form->getName() ) );
// Check if the form is valid.
if ($this->form->isValid())
{
$this->form->save();
// More logic here.
}
}
}
$this->form->bind( $request->getParameter( $this->form->getName() ) );
部分将发布的数据绑定到您的表单,其中$this->form->isValid()
返回一个布尔值,表明该表单是否有效.
The $this->form->bind( $request->getParameter( $this->form->getName() ) );
part binds posted data to your form where $this->form->isValid()
returns a boolean whether the form is valid or not.
这篇关于Symfony提交到相同的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!