php – Symfony2.4表单’此表单不应包含额外字段’错误
作者:互联网
我正在尝试基于REST api ang AngularJS构建应用程序.我一直在关注这个教程http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html但是必须更改一些细节(折旧方法),现在当我发布创建新实体时,我得到’此表单不应包含额外字段’错误.
class MainController extends Controller
{
public function indexAction(Request $request)
{
$form = $this->createForm(new TaskType(),null,array('action' => $this->generateUrl('post_tasks').'.json'))
->add('submit','submit');
$note_form = $this->createForm(new NoteType())
->add('submit','submit');
return $this->render('MyBundle:Main:index.html.twig',
array(
'form'=>$form->createView(),
'note_form'=>$note_form->createView(),
)
);
}
}
我的TaskType表单:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('timeStart','datetime',array(
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'date_format' => 'yyyy-MM-dd',
'data' => new \DateTime('now')
))
->add('timeStop','datetime',array(
'date_widget' => 'single_text',
'time_widget' => 'single_text',
'date_format' => 'yyyy-MM-dd',
'data' => new \DateTime('now')
))
->add('project')
->add('descriptionTask')
->add('isCompleted',null,array('required' => false))
->add('isVisible',null,array('required' => false))
;
}
现在在我看来我只渲染一种形式,但我正处于测试阶段:
{%extends 'MyBundle::layout.html.twig' %}
{%block content %}
<div ng-view></div>
{{ form(form) }}
{% endblock %}
这是REST控制器,它应该刷新给定的实体:
public function cpostAction(Request $request)
{
$entity = new Task();
$form = $this->createForm(new TaskType(), $entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirectView(
$this->generateUrl(
'get_organisation',
array('id' => $entity->getId())
),
Codes::HTTP_CREATED
);
}
return array(
'form' => $form,
);
}
WEIRD THING:当我将相同的代码从REST控制器放到MainController时,然后表单被验证并刷新新实体,但不知何故REST控制器抛出错误…
解决方法:
这是因为当您生成表单时,您正在添加提交按钮,但是当您验证它们时,您不是.尝试:
public function cpostAction(Request $request)
{
$entity = new Task();
$form = $this->createForm(new TaskType(), $entity)->add('submit','submit');
...
提交按钮在技术上是一个字段,即使symfony默认情况下不会将其映射到实体属性.因此,当您使用提交按钮生成表单然后提交该表单时,您在验证控制器中生成的表单操作也需要一个提交按钮.
标签:php,angularjs,forms,symfony,symfony-2-4 来源: https://codeday.me/bug/20190929/1830346.html