In a module, I provide a form to the user to select a value from a select. The form allows multiple values and the form updates itself via ajax to provide always one more select field than selected values. Thus it starts with just one field, and each time you select a value in the new select, another one is added.


enter image description here

After selecting two values

enter image description here

The Problem

Now when the user decides to remove one of the values selected by selecting the null value ---, this appears (first value set to ---):

enter image description here

But of course, I want to have the second value now in the first select field and the second one should show the null value.

How is it possible to alter the $form_state array in such a way?

I tried to go through the respective form_state value, get all the "true" selections and set $form_state to the temporary array like

$tables_real_num = 0;
if (isset($form_state['values']['tables']['table'])) {
  $tables_values = $form_state['values']['tables']['table'];
  $i = 0;
  $tables_real_values = array();
  foreach ($tables_values AS $t => $data) {

    if ($tables_values[$t] != '0') {
      $tables_real_values[$i] = $form_state['values']['tables']['table'][$t];
  $form_state['values']['tables']['table'] = $tables_real_values;

But still with this code, the only value appears in the second select element. At some point, my thinking must take a wrong turn, can you help get back onto the right track?

I know, I could just use a single field with multiple select options in this case, as the available options are always the same, but the user needs to be able to select the same value multiple times.

For reference, the part in question is part of a multistep form. It's on page one of that form and thus in the first constructor function. The function retrieves the form values either from the current submit (when editing page one and if an error occurs) or from stored values (if going back from page two). The pastebin version is different in that these values are used internally to create the distinct select fields. In line 29, I try to set the real values to $form_statebut there must be something wrong with that way.

  • Shouldn't $i start at 0? – Neograph734 Nov 24 '14 at 23:13
  • In that case, I would like to start at $i = 1;. However, if that is a restriction for the $form_state variable (which I haven't heard of), it's not a problem to let it start at zero. Still the problem of reindexing remains. – Paul Nov 25 '14 at 7:32
  • Every array starts at 0. In case you set $tables_real_values[1], you are likely to set the second value of the multi value field. Thus the second dropdown. You could as well use $tables_real_values[$i-1], but why not 0? – Neograph734 Nov 25 '14 at 8:58
  • It has been for legacy reasons, I changed it now, array starts with 0. Problem persists. – Paul Nov 25 '14 at 9:35
  • can we have a look at the rest of your form constructor code? – jowan sebastian Nov 25 '14 at 23:11

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.