Form API #ajax not working on link element

Created on 13 October 2017, over 6 years ago
Updated 22 January 2024, 5 months ago

Problem/Motivation

When adding Form API #ajax parameter to a Link type element:

    $form['link'] = [
      '#type' => 'link',
      '#title' => $this->t('AJAX link'),
      '#url' => Url::fromRoute('ajax_example.example_form_ajax', [], ['query' => ['some_parameter' => 'some_value']]),
      '#ajax' => [
        'callback' => [$this, 'ajaxCallback'],
        'wrapper' => $container_id,
      ],
    ];

, form values are not included in the AJAX request, resulting with a confusing error: "An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (50 MB) that this server supports." that has nothing to do with the actual problem and originates from \core\lib\Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber.php.

This bug seriously limits development possibilities when using AJAX API, because the only way a link can influence a form is by adding query parameters to the FormBuilder as in my Ajax Example sandbox , and always having to build a new form with empty storage etc, instead of working with the current form.

Proposed resolution

I checked the request object and when a link has #ajax parameter, the resulting request object has no form values in $request->query->parameters, as opposed to a submit element for example. Solution most likely requires modifying javascript to include form values in the request.

Also it'd be good to change the error message to be more descriptive, as the problem has nothing to do with file uploads.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Ajax 

Last updated less than a minute ago

Created by

🇵🇱Poland Graber

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024