【发布时间】:2014-04-20 09:04:09
【问题描述】:
我有一个简单的表格:
class SignInType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('_username', 'text');
$builder->add('_password', 'password');
$builder->add('Sign in', 'submit');
}
public function getName()
{
return 'sign_in';
}
}
我在“登录”操作中调用:
public function signInAction()
{
$form = $this->createForm(new SignInType(), null,
[ 'action' => $this->generateUrl('my_project_account_sign_in_process') ]
);
但是绑定 POST 请求会导致表单无效,错误为This form should not contain extra fields。这似乎是导致问题的提交按钮 - 我该如何解决这个问题?
public function signInProcessAction(Request $request)
{
$doctrineManager = $this->getDoctrine()->getManager();
$form = $this->createForm(new SignInType());
$form->handleRequest($request);
...
【问题讨论】:
-
请提供您用来展示表格的树枝。 createAction 的含义是什么?
-
@Healkiss 我的 Twig 视图只包含
{{ form(form) }}。抱歉,createAction是我转换示例时的拼写错误 - 现在已修复。 -
$this->createForm(new SignUpType(), new SignUp())是另一个拼写错误吗?因为我们不能确定 SignUpType === SignInType. -
好的,一些评论:为什么你的 SignInType 没有名字?您在哪里修改表单的操作?我从来没有见过你这样做的“方式”。
-
首先,在你的类型
$builder->setAction($this->generateUrl('my_project_account_sign_in_process'))或者你的twig中添加{{ form(form, {'action': path('my_project_account_sign_in_process'), 'method': 'POST'}) }}的动作设置你的表单提交后你确定在你的signInProcessAction吗?