Validation error is not displayed when uploaded file exceeds server's max_upload_filesize

Created on 23 May 2024, about 1 month ago
Updated 27 May 2024, about 1 month ago

Problem/Motivation

When the filesize reaches the max_upload_filesize value, there is no validation error displayed. There is a missing piece either in core or a webform module. AJAX error defined in docroot/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php is not being displayed at all when AJAX is enabled on webform. Instead the error is displayed in browser console.

Steps to reproduce

  1. Create a webform with just one field of type file.
  2. In the field configuration, limit upload size to 100 MB
  3. Set max_upload_filesize to 120 MB
  4. Try to upload a file with a size between 100 MB and 120 MB - validation works properly and error message is displayed.
  5. Try to upload another file again, but with a size over 120 MB (over server limit) - nothing is displayed, just an AJAX error in console

Proposed resolution

The messenger error coming from core should be displayed on the webform.

    // Render a nice error message in case we have a file upload which exceeds
    // the configured upload limit.
    if ($exception instanceof BrokenPostRequestException && $request->query->has(FormBuilderInterface::AJAX_FORM_REQUEST)) {
      $this->messenger->addError($this->t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', [
        '@size' => ByteSizeMarkup::create((int) $exception->getSize()),
      ]));
      $response = new AjaxResponse(NULL, 200);
      $status_messages = ['#type' => 'status_messages'];
      $response->addCommand(new PrependCommand(NULL, $status_messages));
      $event->allowCustomResponseCode();
      $event->setResponse($response);
      return;
    }

Source: docroot/core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php

Related topics

πŸ› Bug report
Status

Active

Version

6.2

Component

Code

Created by

πŸ‡΅πŸ‡±Poland piotrkonefal

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024