Get an Un-submitted/Active Form Field Value in $form_state? I am unsure how to get unsubmitted/active form field values from a number/textfield into $form_state. field_count could be saved as 4, then is changed to 115, how do I pass that change to $form_state before a submit during $form_state-&gt;setRebuild(TRUE);?

/**
 * {@inheritdoc}
 */
public function buildForm(array $form, FormStateInterface $form_state) {

  $config = $this-&gt;config('MYMODULE.settings');

  $form['#tree'] = TRUE;

  $form['fieldset'] = [
    '#type' =&gt; 'fieldset',
  ];

  $form['fieldset']['field_count'] = [
    '#type' =&gt; 'number',
    '#default_value' =&gt; $config-&gt;get('field_count') ?: 1,
  ];

  // This doesnt get a usable value.
  $form_state-&gt;set('field_count_state', $form['fieldset']['field_count']);

  $form['fieldset']['rebuild'] = [
    '#type' =&gt; 'submit',
    '#value' =&gt; 'Rebuild',
    '#submit' =&gt; ['::rebuildFormSubmit'],
  ];

  $form['fieldset'] = [
    '#type' =&gt; 'fieldset',
  ];

  for ($i = 0; $i &lt; $config-&gt;get('field_count'); $i++) {

    $form['fieldset']['field'][$i] = [
      '#type' =&gt; 'textfield',
      '#default_value' =&gt; $config-&gt;get('field')[$i] ?: '',
    ];

  }

  return parent::buildForm($form, $form_state);

}

/**
 * {@inheritdoc}
 */
public function rebuildFormSubmit(array &amp;$form, FormStateInterface $form_state) {
  $form_state-&gt;setRebuild(TRUE);
  // ???.
  $this-&gt;messenger()-&gt;addMessage($form_state-&gt;get('field_count_state'));
}
MYMODULE/src/Form.php:

/**
 * {@inheritdoc}
 */
public function buildForm(array $form, FormStateInterface $form_state) {

  // get form config.
  $config = $this-&gt;config('MYMODULE.settings');

  // Set our count. Used in for loop below.
  if (!empty($form_state-&gt;getValue(['fieldset', 'field_count']))) {
    // get $form_state count value.
    $defaultCount = $form_state-&gt;getValue(['fieldset', 'field_count']);
  }
  else {
    // Get config count value.
    $defaultCount = $config-&gt;get('field_count');
  }

  $form['#tree'] = TRUE;

  // Fieldset item.
  $form['fieldset'] = [
    '#type' =&gt; 'fieldset',
    '#prefix' =&gt; '&lt;div id="fieldset-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  ];

  // Field count item.
  $form['fieldset']['field_count'] = [
    '#type' =&gt; 'number',
    '#title' =&gt; 'Number',
    '#required' =&gt; TRUE,
    '#default_value' =&gt; $config-&gt;get('field_count') ?: 1,
  ];

  // Rebuild submit item.
  $form['fieldset']['rebuild'] = [
    '#type' =&gt; 'submit',
    '#value' =&gt; 'Apply number',
    '#submit' =&gt; ['::rebuildFormSubmit'],
    '#ajax' =&gt; [
      'callback' =&gt; '::fieldCallback',
      'wrapper' =&gt; 'fieldset-wrapper',
    ],
  ];

  // Loop through our count and create form items.
  for ($i = 0; $i &lt; $defaultCount; $i++) {

    $form['fieldset']['field'][$i] = [
      '#type' =&gt; 'textfield',
      '#default_value' =&gt; isset($config-&gt;get('script')[$i]) ? $config-&gt;get('script')[$i] : '',
    ];

  }

  return parent::buildForm($form, $form_state);

}

/**
 * {@inheritdoc}
 */
public function fieldCallback(array &amp;$form, FormStateInterface $form_state) {
  return $form['fieldset'];
}

/**
 * {@inheritdoc}
 */
public function rebuildFormSubmit(array &amp;$form, FormStateInterface $form_state) {
  $form_state-&gt;setRebuild(TRUE);
}


This part in the $form['fieldset']['rebuild']:

'#ajax' =&gt; [
  'callback' =&gt; '::fieldCallback',
  'wrapper' =&gt; 'fieldset-wrapper',
],


along with:

/**
 * {@inheritdoc}
 */
public function fieldCallback(array &amp;$form, FormStateInterface $form_state) {
  return $form['fieldset'];
}


Are the ajax options.