AJAX forms should submit to $form['#action'] instead of <current>

Created on 10 June 2015, over 9 years ago
Updated 30 August 2024, 5 months ago

Follow-up to #2263569: Bypass form caching by default for forms using #ajax. β†’ .

There, in RenderElement::preRenderAjaxForm() we changed the default URL of an AJAX submission from Url::fromRoute('system.ajax') to Url::fromRoute('<current>'). Most of the time, this is the same as $form['#action'], but not always. For example, comment forms sometimes set the action to a separate page.

For the cases where the action is not the current page, let's discuss which one AJAX should submit to, and then add docs and test coverage for that. In most cases, since the AJAX response is built solely out of what's in $form and $form_state at the end of form processing, which route we submit to probably doesn't result in any difference, if both the current route and action route end up calling $form_builder->buildForm() with the same arguments, and if there aren't alter hooks that modify differently based on the route. But, since it is possible to write routes and alter implementations that result in differences, we should take a stance here.

Reasons for AJAX to submit to $form['#action']:

  • All #ajax elements fall back to submitting to the form action when JS is disabled, so this would bring more consistency for what server-side form processing code runs, regardless of whether JS is enabled in the browser.

Reasons for AJAX to submit to the current page:

  • The purpose of the AJAX response is to update the current page, so shouldn't it do so with content meant for that page? When JS is disabled, an entire new page is returned, so it matters less whether that's the same or different than the current one.
πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
AjaxΒ  β†’

Last updated 1 day ago

Created by

πŸ‡ΊπŸ‡ΈUnited States effulgentsia

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.71.5 2024