0

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'));
}
1
  • 2
    With the #tree option you should find the field value in $form_state->getValue(['fieldset', 'field_count']);
    – 4uk4
    Mar 19, 2019 at 22:05

1 Answer 1

1

MYMODULE/src/Form.php:

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

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

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

  $form['#tree'] = TRUE;

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

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

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

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

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

  }

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

}

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

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

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

'#ajax' => [
  'callback' => '::fieldCallback',
  'wrapper' => 'fieldset-wrapper',
],

along with:

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

Are the ajax options.

2
  • 1
    The last line by itself is not correct, try: '#default_value' => isset($config->get('field')[$i]) ? $config->get('field')[$i] : '',. Other than that, a form should work without #ajax as well, because this is the fallback for non-js clients.
    – 4uk4
    Mar 20, 2019 at 7:30
  • Thank you, yes that modification resolves the Undefined index notices when not using ajax in the form. I have updated the answer. Mar 20, 2019 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.