Symfony 2 form extra fields

12,640

Solution 1

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

Solution 2

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.

Share:
12,640
Pawel
Author by

Pawel

Updated on June 16, 2022

Comments

  • Pawel
    Pawel almost 2 years

    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.