我有一个面板,上面有3个锚点。每个锚点打开不同的表单,这些表单由我的主控制器中的不同操作处理。在树枝上,我叫
{{render(controller('AppBundle:Home/Home:changePersonalInfo')) }}
这是在trick:中调用的操作
public function changePersonalInfoAction(Request $request){
$user = $this->getUser();
$usertype = new UserType($this->generateUrl('changedetails'));
$formAccount = $this->createForm($usertype, $user);
if($request->isMethod("POST"))
{
$formAccount->handleRequest($request);
if($formAccount->isValid())
{
$data = $formAccount->getData();
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($data);
$entityManager->flush();
}
}
return array(
'formAccount' => $formAccount->createView(),
);
}
在这个操作中,我生成一个URL,然后将其传递给userType类中的formbuilder:
class UserType extends AbstractType
{
private $target_action;
/**
* @param Router
*/
public function __construct($target_action)
{
$this->target_action = $target_action;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setAction($this->target_action);
这样表单就可以知道应该提交给哪个操作。然而,当我提交表格时,我会被重定向到
/home%23change_details
而不是
/home#change_details
只有当我在控制器之外的任何其他地方构建表单时才会发生这种情况(我试图通过放置在trick中进行此操作
<form action="{{ path(mypath) }}" ...
在表单块中,它产生了相同的输出)当我在控制器中构建表单时,一切都很顺利。有人知道怎么解决这个问题吗?
您可以从控制器中将操作设置为选项。无需将其作为构造函数参数传递:
$formAccount = $this->createForm($usertype,$user,array(
'action'=>$this->generateUrl('changedetails'));