I am struggling to find a solution to the following, using drupal 7 and hook_form_alter.

Within my form I have a group of text inputs, the number of which can be altered by the user in 5s. i.e. there can be 5, 10, 15 ... n*5 inputs. Not all of these are required to be filled and I would like to count how many have been used and return this to the user as they are filling the form in. This eventually will be used in a drop down, however that will have to wait for another question. For now I am just looking to return the number.

The inputs are built in a function that uses an array in the $form_state for both number and value for each of the textfields.

I thought that by implementing ajax on each of the fields I could then count and return an array pair to be stored with the initial array (differentiated from the others as a string key and not integer), by updating the $form_state, and this could then be used to dynamically update the select options or in this case an html element with the number. The form state is being altered in the callback but is not then altering the $form_state which the inputs are being built from.

Any suggestions on how I could get this to work, or other solutions greatly appreciated.

Code to build textfields where $item is array of specified within $form_state

foreach ($item as $ikey => $value) {
            $form[$section][$area][$areacont][$areabox][$ikey] = array(
              '#type' => 'textfield',
              '#size' => 20,
              '#default_value' => $value,
              '#ajax' => array(
                    'callback' => 'bform_commands_count_callback',
                    'wrapper' =>  $section . '-' . $area,
                    'progress' => array('type' => 'none'),
                    ), 
            );      
            }

Callback

function bform_commands_count_callback ($form, &$form_state){
$trigger = $form_state['triggering_element']['#array_parents'];
$section = $trigger[0];
$type = strstr($section, 'section', TRUE);
    $area = $trigger[1];
$ins = $section . '-' . $area . '-ins';
    $values = $form_state['input'];
    $numfilled = count(array_filter($values[$ins]));

$form_state['ins'][$type][$ins] = array('numfilled' => $numfilled) + $values[$ins];
$form_state['rebuild'] = TRUE;
return $form[$section][$area];
}
  • Alternatively if I use ajax commands e.g. ajax_command_data I can see the command being sent but how do I then get this to update the $form_state? – Inigo Montoya Jun 11 '14 at 8:55
  • Fixed this using ['#ajax]['trigger_as'] on the textfield to point to a hidden button which was then able to execute a submit function and handled the change in $form_state and $form_state['rebuild'] – Inigo Montoya Jun 18 '14 at 12:01

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.