Drupal Forms using ajax breaking dependency injection

Profile picture for user Phil Frilling
By Phil Frilling, 7 April, 2021

Recently, I was building a custom Drupal form that used dependency injection and included an ajax submit handler. While testing the form, on the initial page load, the form submitted just fine using ajax. However, when the user used another ajax handler on the same form, all of the injected services were set to `NULL`.

It turns out that I had my services set to be

 private $myService; 
, which were not able to be serialized in the form. Changing these to
 protected $myService;
solved the issue.

Thanks for the assist: https://drupal.stackexchange.com/questions/287289/form-state-cache-brea…