Problem/Motivation

When a Drupal form uses AJAX, member variables lose their values when submitForm() is invoked.

Steps to reproduce

  1. Create a custom form with AJAX
  2. Add a private RendererInterface member variable to the class
  3. Use dependency injection to initialize the variable in the constructor
  4. In the submitForm() method display a message indicating whether the member variable's value is empty or set
  5. Load the form and submit it
  6. Read the message displayed by submitForm()

Proposed resolution

Ensure that the object handling the submitForm() method is a properly initialized instance of the custom form class.

Remaining tasks

Fix the bug.

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Release notes snippet

Fixed bug in initialization of form objects with AJAX.

CommentFileSizeAuthor
#2 form_mystery.tgz1.59 KBbkline

Comments

bkline@rksystems.com created an issue. See original summary.

bkline’s picture

StatusFileSize
new1.59 KB

I have provided a repro case demonstrating the steps given above.

cilefen’s picture

Status: Active » Postponed (maintainer needs more info)

I think this may be more of a support request. Does https://drupal.stackexchange.com/questions/231436/form-ajax-not-working-... explain what you are seeing?

bkline’s picture

I don't think so. I did, however, get some insight from Conrad Lara (@cmlara) in the Slack #support channel. Apparently Drupal sometimes (but not always) serializes the instance of the FormBase-derived class and then reconstitutes most of it, invoking submitForm() on this partially-resurrected instance. In the process it drops the private member variables on the floor. "Sometimes" likely means "when AJAX is in play" in this context. As far as Conrad is aware, this behavior isn't documented anywhere. But sure enough, when I replace private with protected in the repro code I posted the form works correctly. I will edit the original issue summary to clarify that it's private member variables which are discarded.

bkline’s picture

Title: Form class member variable null in submitForm when form uses AJAX » Form class private member variable null in submitForm when form uses AJAX
Issue summary: View changes
ddavisboxleitner’s picture

This has been an issue for us as well. Switching all private variables to protected works to preserve the data in those variables during AJAX calls.

There is documentation about the serialization issue: https://www.drupal.org/docs/drupal-apis/services-and-dependency-injectio...

When service objects are seralized by drupal private properties are lost.

Be sure to not set any service properties in your form class to have private visibility.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Should this be re-opened? If no response in 3 months will close out.

bkline’s picture

The status says "Maintainer needs more info" but it's not clear what additional information is needed. The behavior doesn't match the documentation. That's the definition of a bug. Data loss in most contexts means serious bug.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Active

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.