Skip to content

Consider add a configuration for ignore form extra data #7209

Closed
@marcospassos

Description

@marcospassos

Most of RESTFull API's just ignores extra data submitted to their actions because in some cases doesn't make any sense show an error, mainly when you are working with unnamed forms. When you don't have subforms, a terrible workaround with problematic performance is:

    $data = $request->request->all(); // ->get($form->getName());
    $children = $form->all();
    $data = array_intersect_key($data, $children);
    $form->bind($data);

So, I suggest introducing a new simple option in the form configuration:

$factory->create('acme.form', $model, array('ignore_extra_data' => true));
        // Mark the form with an error if it contains extra fields
        if (false == $config->getOption('ignore_extra_data') && count($form->getExtraData()) > 0) {
            $this->context->addViolation(
                $config->getOption('extra_fields_message'),
                array('{{ extra_fields }}' => implode('", "', array_keys($form->getExtraData()))),
                $form->getExtraData()
            );
        }

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php#L108

Once the default value is false, it'll not cause BC breaks.

If you agree, I'll be happy in submit a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions