Symfony2.4 表单“此表单不应包含额外字段”错误
2022-08-30 22:37:54
我正在尝试构建基于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(),
)
);
}
}
我的任务类型表单:
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,
);
}
奇怪的事情:当我将相同的代码从REST控制器放入MainController时,表单被验证并且新实体正在被刷新,但不知何故REST控制器抛出错误...