0

SOLVED (see comments)

I have a form with a select field (where you can select a title of a novel). This select field automatically writes a the respective ISBN of the novel into a textfield when a title of the novel is chosen by the user. The user can add more novels via a button.

$form['fieldset-books'] = [
  '#type' => 'fieldset',
  '#title' => t('Books'),
  '#prefix' => '<div id="books-fieldset-wrapper">',
  '#suffix' => ' </div>',
];

if (empty($form_state['count_novels'])) {
  $form_state['count_novels'] = 1;
}

for ($i = 0; $i < $form_state['count_novels']; $i++) {

  $form['fieldset-books']['fieldset-novel'][$i] = [
          '#type' => 'fieldset',
          '#title' => t('Novel'),
  ];
  $form['fieldset-books']['fieldset-novel'][$i]['title'] = [
    '#type' => 'select',
    '#title' => t('Title'),
    '#empty_value' => '',

    '#options' => NOVEL_TITLE,
    '#ajax' => [
      'callback' => 'ajax_callback',
      'wrapper' => 'books-fieldset-wrapper',
      'event' => 'change',
    ],
  ];

  $form['fieldset-books']['fieldset-novels'][$i]['isbn'] = [
    '#type' => 'textfield',
    '#title' => t('ISBN'),
    '#attributes' => [
      'placeholder' => t('- will be automatically assigned -'),
      'readonly' => 'readonly',
    ],
  ];
}

$form['fieldset-books']['novel_add'] = [
  '#type' => 'submit',
  '#value' => t('Add Novel'),
  '#name' => 'AddNovelButton',
  '#submit' => ['ajax_submit'],
  '#ajax' => [
    'callback' => 'ajax_callback',
    'wrapper' => 'books-fieldset-wrapper',
  ],
  '#limit_validation_errors' => [],
  '#attributes' => ['class' => ['btn-primary btn-sm']],
];

function ajax_callback($form, $form_state) {
  if ($form_state['triggering_element']['#name'] == 'AddNovelButton') {
    return $form['fieldset-books'];
  }
  elseif ($form_state['triggering_element']['#name'] == 'DiseaseSelect') {

    for ($i = 0; $i < $form_state['count_novels']; $i++) {
      if (isset($form_state['values']['fieldset-books']['fieldset-novel'][$i]['title'])) {
        $isbn= ISBN[array_search($form_state['values']['fieldset-books']['fieldset-novel'][$i]['title'],
          array_keys(TITLE))];
      }
      else {
        $isbn= NULL;
      }
      $form['fieldset-books']['fieldset-novels'][$i]['isbn']['#value'] = $isbn;
    }
    return $form['fieldset-books'];
  }
  $form_state['rebuild'] = TRUE;
  return NULL;
}

function ajax_submit($form, &$form_state) {
if ($form_state['triggering_element']['#name'] == 'AddNovelButton') {
    if (!isset($form_state['count_novels'])) {
      $form_state['count_novels'] = 0;
      $form_state['count_novels']++;
    }
    $form_state['count_novels']++;
  }
  $form_state['rebuild'] = TRUE;
}

The problem is, that the $form_state of the novel title is not saved / reset when the user picked a novel title from the select list. After the rebuild the novel title field is empty again (and so is the ISBN field).

I tried to find the bug by reducing the code /complexity (e.g. removing the novel-fieldset) but nothing worked. When setting the $form_state for the novel title manually like

$form_state['values']['fieldset-books']['fieldset-novel'][0]['title'] = 'Robinson Crusoe';

The correct ISBN gets resolved after the rebuild but the title field is empty again.

Any idea, what i did wrong?

  • Hmm, it seems, the problem is not even AJAX related. If i remove the AJAX par from the title select field, the problem persists, – Xyarvius Apr 8 at 15:00
  • 1
    Hmm, i found out, that '#name' is causing this issue. A select form is not allowed to have a a '#name'. – Xyarvius Apr 8 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.