Skip to content

[Validator] Check post_max_size before validating #11729

@rvanlaak

Description

@rvanlaak

The File maxSize Constraint does not seem to work when post_max_size has been reached. The request method is post, but the rest of all variables (including $files) is empty.

When the post is too big, the form will try to get validated via $form->isValid() but since there are no values this goes wrong. Or to be more precise, nothing happens because the form was not marked as submitted.

Currently I did fix this by calling the following snippet before calling $form->isValid():

    // First check or the filesize was too large, because in that case files global is empty
    $params = new ServerParams();
    if ($params->getContentLength() > $params->getPostMaxSize()) {
        $form->get('file')->addError(
            new FormError($this->get('translator')->trans('form.msg_max_post',
                array(
                    '%contentSize%' => $document->formatFilesize($params->getContentLength()),
                    '%maxSize%'     => '20 MB',
                ),
                'validators'
            ))
        );
    }

Symfony has a FormValidator, which even has a similar check like the above snippet.

My above snippet does work properly, but after commenting it, for the same request FormValidator->validate() is not executed when the post is too large.

It seems to go wrong in the HttpFoundationRequestHandler where $request->request and $request->files are both empty.

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