16

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

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 %}

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

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...

4

5 回答 5

30

如果您希望验证器忽略其他字段,您应该尝试将'allow_extra_fields' => true其作为选项传递给 FormBuilder。

于 2015-07-29T23:41:00.137 回答
18

这是因为当您生成表单时,您正在添加提交按钮,但是当您验证它们时,您却没有。尝试:

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

提交按钮在技术上是一个字段,即使默认情况下 symfony 不会将其映射到实体属性。因此,当您使用提交按钮生成表单然后提交该表单时,您在验证控制器操作中生成的表单也需要有一个提交按钮。

于 2014-04-11T18:01:34.340 回答
3

如果要禁用字段验证,则必须添加

public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'csrf_protection' => false,
        'validation_groups' => false,
    ));
}

在 buildForm 方法中:

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) {
            $event->stopPropagation();
        }, 900);
        $builder->add('field1','text')
                ->add('field2','text')
                .
                .
                .
    } 

更多详情:http ://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-dynamic-form-modification-suppressing-form-validation

于 2014-07-30T08:13:00.413 回答
3

在 CustomFormType 中使用 Symfony 4/5

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => CustomFormType::class,
        'allow_extra_fields' => true // <<<<<<<<<<<<<<<<<<<<<
    ]);
}
于 2020-07-06T09:17:58.057 回答
1

如果要添加单个 SubmitType 按钮或类似按钮,则使用 Chausser 指示的解决方案

$entity = new Task();
$form = $this->createForm(new TaskType(), $entity)->add('submit','SubmitType::class');

但是,如果您使用 CollectionType 并嵌入一组子表单,则需要在该类型的参数中包含 'allow_add' => true 。例如,在您的 EntityType 表单构建器中:

$builder->add('subEntities', CollectionType::class, array(
                'entry_type' => SubEntityType::class,
                'entry_options' => array('label' => false),
                'allow_add' => true,
            ))

如果您有多个级别,请确保对所有级别的嵌入执行此操作。

于 2018-10-18T20:29:42.247 回答