Why can't I change textfield #default_value based on checkboxes' values after an AJAX response? I have been trying to learn Drupal forms with AJAX recently and from my current understanding of it I thought I should be able to do something like presented below (I wanted to store all selected values as a string in another hidden field to grab it later in my JS and do some stuff, but that does not matter here, what matters is what I do not get about AJAX).

So the idea is that selecting checkboxes will update the value in the input field with checkboxes' values.

Unfortunately, the #default_value on my textfield is not being set on my page, even though I can see it being set in my $form array when debugging with xdebug.

The form is used to filter some results with an AjaxResponse() ReplaceCommand.

Here is the simplified code, stripped from most unrelated things:

public function buildForm(array $form, FormStateInterface $formState)  {
  $values = $formState-&gt;getValues();
  $defaultCountry = implode(':', array_filter($values['countryId']));
  $form['countryId'] = [
    '#type' =&gt; 'checkboxes',
    '#title' =&gt; $this-&gt;t('Country'),
    '#options' =&gt; [
      '1' =&gt; 'Aaa',
      '2' =&gt; 'Bbb',
      '3' =&gt; 'Ccc',
    ],
    '#ajax' =&gt; [
      'callback' =&gt; '::ajaxResponse',
      'event' =&gt; 'change',
      'wrapper' =&gt; 'some-wrapper-for-filters-form-and-results',
      'progress' =&gt; array(
        'type' =&gt; 'throbber',
        'message' =&gt; NULL,
      ),
    ],
  ];
  $form['defaultCountry'] = [
    '#type' =&gt; 'textfield',
    '#title' =&gt; $this-&gt;t('TEST'),
    '#default_value' =&gt; $defaultCountry,
  ];
  return $form;
}

public function ajaxResponse(array &amp;$form, FormStateInterface $formState) {
  $result = 'some processing logic';
  $response = new AjaxResponse();
  $response
    -&gt;addCommand(new ReplaceCommand('.filtered-offers &gt; div', $result));
  return $response;
}


What do I miss here?
While I'm not sure why it doesn't work, the fix I've found in the past is to set the default value as #value in the AJAX callback. While this generally goes against the principle of #AJAX in Drupal (ie - changes should be made in the form definition), it works.