How to save entity from a ajax form I'm running Drupal 8.8.x

The Problem

Entity is not being saved when form is submitted through AJAX. Example:

Build Form

public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity = NULL) {
  if($entity) {
    $this-&gt;entity = $entity;


    $form['element'] = [
      '#type' =&gt; 'textfield',
      '#title' =&gt; $this-&gt;t('My title'),
      '#name' =&gt; 'element',
      '#ajax' =&gt; [
         'event' =&gt; 'focusout',
         'callback' =&gt; [$this, 'myCallback'],
         'disable-refocus' =&gt; TRUE
      ]
    ];
  }

  return $form;
}


Submit Form

public function submitForm(array &amp;$form, FormStateInterface $form_state) {
 $element = $form_state-&gt;getTriggeringElement()['#name'];
 $value = $form_state-&gt;getValue($element);

 $this-&gt;entity-&gt;set($element, $value);
 $this-&gt;entity-&gt;save();
}


I've confirmed $element and $value are the correct key and value. However, when I save the entity nothing happens. There is nothing updated in the database. And there are no errors emitted in the console or watchdog.

If anyone knows what could be the issue I'd appreciate some insight! :)
Drupal 8 doesn't cache Ajax forms anymore, so you can't get properties of the form object set in the initial form build. see Bypass form caching by default for forms using #ajax.

Forms are only cached as rendered form. Put the entity id in a hidden form element to load the entity before saving. Add the entity cache tag to the form if you use default values from the entity.

Or consider to switch from a general Form API form to an entity form.