Using GET Method On Form Elements With Ajax Callback

Created on 24 April 2024, 8 months ago
Updated 3 July 2024, 6 months ago

Problem/Motivation

Non-ajax response is returned from the Ajax callback of form elements when "httpMethod" property is set to "GET". This is likely due to the Form Builder class not recognizing the correct request method of the request and causing user input to be loaded from the wrong request property. ($request->query vs $request->request). Because of that, the event subscriber of the form builder is skipped.

Steps to reproduce

Add "a #ajax" callback to any form element in form alter with "httpMethod" set to "GET"

Proposed resolution

Ajax callback should return the same response as using "POST" method.

With the change Ajax request type can be set to GET (POST by default) β†’ in place, we want to improve the performance of our forms which include elements that we have "#ajax" callback added programmatically. The callbacks of these elements are mainly used to refresh a view programmatically based on user's selection. However, after adding the "httpMethod" parameter to the "#ajax" array, the AJAX callback began returning the HTML of the home page instead instead of the old JSON response.

After doing a bit of debugging inside the Core's code, we found that the "formBuilder" class depends on the "isMethodType" method from "formState" class to determine where user input is stored and then determine whether a request is a regular or AJAX request based on if the user input is empty or not.

We then found that the "method" property retains its default value of "POST" regardless if the request is using GET or POST method and we don't see that the property is being set or updated anywhere. The actual method of the HTTP request is stored in another property called "requestMethod". Given our findings, since "isMethodType" is the method that's being used in formBuilder, we believe that the issue we encountered with our custom code's AJAX callback not being triggered stems from the "formBuilder" failing to locate user input in the request, leading it to misidentify the request as a regular one instead of an AJAX request.

I'm not sure if this is something that was overlooked in the previous issue or using GET isn't recommended (or allowed) in custom #ajax callbacks?

Any enlightenment would be appreciated.

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
AjaxΒ  β†’

Last updated about 10 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States sker101 NYC

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

Comments & Activities

Production build 0.71.5 2024