Symfony2.4 form 'This form should not contain extra fields' error

前端 未结 5 666
忘了有多久
忘了有多久 2020-12-10 12:17

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 cha

相关标签:
5条回答
  • 2020-12-10 12:41

    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');
        ...
    

    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.

    0 讨论(0)
  • 2020-12-10 12:47

    If you wanna disable fields validation, you must add

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

    And in buildForm method:

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

    For more details: http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-dynamic-form-modification-suppressing-form-validation

    0 讨论(0)
  • 2020-12-10 12:51

    If you are adding a single SubmitType button or similar, you use the solution Chausser indicated

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

    However, in case you are using a CollectionType and embedding a set of sub forms, you need to include 'allow_add' => true in your parameters for that type. For example, in your EntityType form builder:

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

    Make sure you do that for all levels of embedding if you have multiple levels.

    0 讨论(0)
  • 2020-12-10 12:53

    Working in Symfony 4/5 in CustomFormType

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => CustomFormType::class,
            'allow_extra_fields' => true // <<<<<<<<<<<<<<<<<<<<<
        ]);
    }
    
    0 讨论(0)
  • 2020-12-10 13:03

    If you want the validator to ignore additional fields you should try passing 'allow_extra_fields' => true as an option to the FormBuilder.

    0 讨论(0)
提交回复
热议问题