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->setRebuild(TRUE);
?
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('MYMODULE.settings');
$form['#tree'] = TRUE;
$form['fieldset'] = [
'#type' => 'fieldset',
];
$form['fieldset']['field_count'] = [
'#type' => 'number',
'#default_value' => $config->get('field_count') ?: 1,
];
// This doesnt get a usable value.
$form_state->set('field_count_state', $form['fieldset']['field_count']);
$form['fieldset']['rebuild'] = [
'#type' => 'submit',
'#value' => 'Rebuild',
'#submit' => ['::rebuildFormSubmit'],
];
$form['fieldset'] = [
'#type' => 'fieldset',
];
for ($i = 0; $i < $config->get('field_count'); $i++) {
$form['fieldset']['field'][$i] = [
'#type' => 'textfield',
'#default_value' => $config->get('field')[$i] ?: '',
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function rebuildFormSubmit(array &$form, FormStateInterface $form_state) {
$form_state->setRebuild(TRUE);
// ???.
$this->messenger()->addMessage($form_state->get('field_count_state'));
}
$form_state->getValue(['fieldset', 'field_count']);