5

So i'm trying to submit a form using ajax, in my form i have the following:

        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => $this->t('OK'),
        '#button_type' => 'primary',
        '#prefix' => '<div class="tc btn_pay relative">',
        '#suffix' => '</div>
                        </div>',
        '#attributes' => array(
            'class' => array('proximabold', 'tc', 'transition', 'no-underline', 'ph3', 'pv2', 'mb2', 'dib', 'white'),
        ),
        '#ajax' => [
            'event' => 'click',
            'callback' => '::updateTable',
            'wrapper' => 'table-wrapper',
            'progress' => [
                'type' => 'throbber',
                'message' => t('Verifying entry...'),
            ],
        ],
    );

and a simple updateTable function:

public function updateTable(array $form, FormStateInterface $form_state) {
    return "Hello World";
}

But when i click on submit i get the error: An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (2 MB) that this server supports.

I knew that the error is server side (Maybe i'm wrong), so i went to php.ini (i use PHP7) and update the following lines:

post_max_size = 6144M
memory_limit = 6144M
upload_max_filesize = 6144M
max_file_uploads = 1000

but without luck, this configuration shouldn't be necessary as the request size is only 1.5KB

1
  • i found that the mod_security module create some obstacles for ajax submission but i don't even have the latter installed!! Commented Nov 12, 2017 at 17:38

2 Answers 2

9

By debugging the code I found the solution. The error was triggered from core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php

if ($exception instanceof BrokenPostRequestException && $request->query->has(FormBuilderInterface::AJAX_FORM_REQUEST)) {
  $this->drupalSetMessage($this->t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', ['@size' => $this->formatSize($exception->getSize())]), 'error');
  $response = new AjaxResponse();
  $status_messages = ['#type' => 'status_messages'];
  $response->addCommand(new ReplaceCommand(NULL, $status_messages));
  $response->headers->set('X-Status-Code', 200);
  $event->setResponse($response);
  return;
}

The error triggered if the BrokenPostRequestException exception is thrown.

Now if we got to core/lib/Drupal/Core/Form/FormBuilder.php we find that this exception is thrown if the post request is bigger than what is allowed by the server and also if the request doesn't carry the form id.

if ($ajax_form_request && !$request->request->has('form_id')) {
  throw new BrokenPostRequestException($this->getFileUploadMaxSize());
}

It would be better if there was another exception for this case rather than getting the same error for when the post request exceeds the configured allowed size (post_max_size)

In my case it was not a matter of post size request, rather I didn't include the form id in my twig file.

I added the lines containing form_id and form_build_id:

<div class="fr relative filtrehold">
        <div class="filtr_btn  br-pill shadow-2 ttu dark-gray f5 proximabold">Filtres</div>
        <form> //Added this one
            {{ McRefund_build_data.filterform.form_build_id }} // Added
            {{ McRefund_build_data.filterform.form_id }} // Added 
            {{ McRefund_build_data.filterform.RelevesMensuels }}
            {{ McRefund_build_data.filterform.from }}
            {{ McRefund_build_data.filterform.to }}
            {{ McRefund_build_data.filterform.example_select }}
            {{ McRefund_build_data.filterform.actions.submit }}
        </form> //Added this one
</div>
<div class="table_hold bg-white br2">

    <div id="table-wrapper">

        {% for Num_refund,refund in McRefund_build_data.filterform.RefundsContainer['Refunds']['#Refunds_data'] %}
                   ............
                   ............
                   ............
        {% endfor %}
    </div>
</div>
5
  • This was super helpful. Just ran in to this. Thank you! Commented Mar 13, 2018 at 3:13
  • 1
    My problem: I was adding a 'destination' parameter to the url of the ajax link that opened my custom form, and used the destination in my submit handler, to issue a form redirect, causing the redirect to appear to be an ajax request in /Drupal/Core/Form/FormBuilder.php:299, but without the form_id (since it was in fact a GET (redirect)), and thus exploded with that misleading upload file size error. My solution: Remove the destination parameter from the ajax link, so as not to confuse the ajax handling of the form builder on the redirect; and handle the redirect a different way. Commented Mar 13, 2018 at 3:25
  • Great job @SteveOliver, glad it helped :D Commented Mar 21, 2018 at 9:43
  • FYI: on my side, it was the opposite=> My themed form was containing the <form...> tag. But as it was inside an inline entity form I got this problem. Removing this tag solved it.
    – Baud
    Commented Apr 20, 2021 at 15:21
  • This is super help.. add form_id, form_build_id to your twig file solved my problem. Many many thanks to you Commented May 12, 2022 at 11:17
6

Due to an identical error message ("An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size that this server supports")in a project based on Drupal 8, I just discovered a third use of the same exception discussed above. In my case, the exception was thrown by reaching limit of allowed variables in the php.ini file.

  1. Open the file -> vim /etc/php/7.2/apache2/php.ini
  2. Locate the line -> /max_input_vars/n
  3. Uncomment the line and put -> max_input_vars = 3000
  4. Restart Apache -> sudo /etc/init.d/apache2 restart

max_input_vars in php.ini

My two Cents.

1
  • 1
    Thank you! I was running into this error on a Webform with 300 options. Increasing max_input_vars fixed the error for me. I'm also on Acquia so I had to increase it via the Acquia dashboard as described here: support.acquia.com/hc/en-us/articles/… Commented May 5, 2021 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.