Symfony2.4 表单“此表单不应包含额外字段"错误 [英] Symfony2.4 form 'This form should not contain extra fields' error

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

问题描述

我正在尝试基于 REST api 和 AngularJS 构建应用程序.我一直在关注本教程 http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html 但必须更改一些细节(折旧方法),现在当我发布以创建新实体时,我收到此表单不应包含额外字段"错误.

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

我的 TaskType 表单:

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

目前在我看来,我只渲染了一种形式,但我正处于测试阶段:

right now in my view I'm rendering only one form BUT I'M IN THE TEST STAGE:

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

奇怪的事情:当我将相同的代码从 REST 控制器放入 MainController 时,表单被验证并且正在刷新新实体,但不知何故 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天全站免登陆