5

Im changing some fields via AJAX and when im trying to save a form i recive a error that Extra fields are not allowed.

How to change that validator property like validatorPass() in sf1.4 ?
Or its possible change to form to accept extra fields ?

Im using SonataAdminBundle to create forms.

j0k
  • 22,600
  • 28
  • 79
  • 90
Pawel
  • 1,672
  • 2
  • 18
  • 24

3 Answers3

20

You could remove the extra fields from the request data before binding them to the form:

    // The JSON PUT data will include all attributes in the entity, even
    // those that are not updateable by the user and are not in the form.
    // We need to remove these extra fields or we will get a
    // "This form should not contain extra fields" Form Error
    $data = $request->request->all();
    $children = $form->all();
    $data = array_intersect_key($data, $children);
    $form->bind($data);
Samuel Katz
  • 24,066
  • 8
  • 71
  • 57
rhunwicks
  • 3,198
  • 24
  • 21
  • In my case I had to change the first line to: $data = $request->request->get($form->getName()); – Sergiy Sokolenko Jun 15 '12 at 15:17
  • Is there a way to get the $request in an eventSubscriber to have this solution work on all forms where the $builder add the eventSubscriber? – Simon Feb 12 '14 at 22:15
  • It's perfect for integrating REST clients, like Restangular or something. Thanks! – Slava Fomin II May 29 '14 at 20:11
  • Thanks did worked for me. I created a function that uses this for my api process form function: https://gist.github.com/ajankowski/ca28218ac1374c286e90 – albertski Jul 22 '15 at 14:44
1

In my case the solution was really simple, just add allow_add to your collection field, below my example

        ->add('Details', 'collection', array(
            'type' => new DetailsType(),
            'allow_add' => true,
            'allow_delete' => true,
            'label' => ' '
        ))

You can also check the official documentation for this issue http://symfony.com/doc/current/cookbook/form/form_collections.html

The first thing you need to do is to let the form collection know that it will receive an unknown number of tags. So far you've added two tags and the form type expects to receive exactly two, otherwise an error will be thrown: This form should not contain extra fields. To make this flexible, add the allow_add option to your collection field.

0

You can't add extra fields since they aren't declared into the entity. There is a solution to bypass your problem:

  • create a dynamic form where you can add extra fields.

You have an example on how it work on github: https://github.com/Keirua/KeiruaProdCustomerDemoBundle

and the complete tutorial at this address (but in French):

http://blog.keiruaprod.fr/2012/01/18/formulaires-dynamiques-avec-symfony2/

PS: It seems Sonata uses this way to add fields.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Chopchop
  • 2,899
  • 18
  • 36
  • Hmm.. fields are related to entity by many to many. But i want on the list only elements which are related to one category. – Pawel Jan 31 '12 at 12:38
  • The relation used is OneToMany since you create à new element that will be link to the category. – Chopchop Feb 13 '12 at 11:19