Symfony2.4形式“这种形式不应包含额外的字段”错误 [英] Symfony2.4 form 'This form should not contain extra fields' error

查看:205
本文介绍了Symfony2.4形式“这种形式不应包含额外的字段”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于REST API昂AngularJS构建应用程序。我一直在关注这个教程<一个href=\"http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html\">http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html但必须改变一些细节(德preciated方法),而现在,当我张贴创建新的实体,我得到'这种形式不应该包含额外的字段的错误。

I'm trying to build app based on REST api ang AngularJS. I've been following this tutorial http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html but have to change some details ( depreciated methods ) and right now when I post to create new entity I get 'This form should not contain extra fields' error.

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(),
                )
        );
    }
}

我的任务类型的形式:

my TaskType form:

 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控制器,其被认为以刷新指定的实体:

AND this is the REST controller which is supposed to flush given entity:

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,
 );
}

奇怪的事情:当我把从静止控制器MainController同一code,然后形式进行验证并新实体被刷新,但不知何故,REST控制器抛出错误...

WEIRD THING: when I put the same code from REST controller to MainController, then form is validated and new entity is being flushed, but somehow REST controller throws error...

推荐答案

它,因为当你正在生成要添加的提交按钮的形式,但是当您要验证他们你是不是。尝试:

Its because when you are generating the form you are adding submit buttons but when you are validating them you are not. try:

public function cpostAction(Request $request)
{
    $entity = new Task();
    $form = $this->createForm(new TaskType(), $entity)->add('submit','submit');
    ...

提交按钮在技术上是即使symfony的习惯它映射到默认的实体属性的字段。所以,当你生成一个提交按钮,然后提交该表格您在验证控制器动作生成表单的形式需要也提交按钮。

The submit button is technically a field even though symfony wont map it to an entity property by default. So when you generate the form with a submit button and then submit that form the form you generate in your validation controller action needs to also have a submit button.

这篇关于Symfony2.4形式“这种形式不应包含额外的字段”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆