0

Goal Provide a user with a drop down menu defaulted to a value of none. Other options are the number 1 through 27. Based on the value select have drupal FAPI generate a fieldset that has anywhere from 1 to 27 required text fields. And should they then change their mind it would re-render accordingly.

What I have So far I am able to get the FAPI to hide the field on initial form render however I can't seem to figure out how I'd take the selected array value and use that to generate out a new field set dynamically.

function single_add_ucm_form($form, &$form_state) {
$form['number_subscribers'] = array(
'#type' => 'select',
'#title' => t('How many subscribers are in the CUCM cluster?'),
'#required' => TRUE,
'#default_value' =>  0,
'#ajax' => array(
  'callback' => 'ajax_select_callback',
  'wrapper' => 'replace_subscribers_div',
  'effect' => 'slide',
 ),
'#options' => array(
  0 => t('none'),
  1 => t('1'),
  2 => t('2'),
  3 => t('3'),
  4 => t('4'),
  5 => t('5'),
  6 => t('6'),
  7 => t('7'),
  8 => t('8'),
  9 => t('9'),
  10 => t('10'),
  11 => t('11'),
  12 => t('12'),
  13 => t('13'),
  14 => t('14'),
  15 => t('15'),
  16 => t('16'),
  17 => t('17'),
  18 => t('18'),
  19 => t('19'),
  20 => t('20'),
  21 => t('21'),
  22 => t('22'),
  23 => t('23'),
  24 => t('24'),
  25 => t('25'),
  27 => t('26'),
  27 => t('27'),
),
);
$form ['subscribers_wrapper_container'] = array(
'#type' => 'container',
'#prefix' => '<div id="replace_subscribers_div">',
'#suffix' => '</div>',
);

if (isset($form_state['values']['number_subscribers'])) {
$form_state >= 1;
$form['subscribers_wrapper_container']['UCM_SUB'] = array(
    '#type' => 'textfield',
    '#size' => 30,
    '#title' => t('Subscriber IP Address'),
    '#required' => TRUE,
  );
}
}


function ajax_select_callback($form, $form_state) {
return $form['subscribers_wrapper_container'];
  }
0
if (!isset($form_state['values']['number_subscribers'])) {
   $form_state['values']['number_subscribers'] = 0;
}

for($i = 0; $i < $form_state['values']['number_subscribers']; $i++){
$form['subscribers_wrapper_container'][]['UCM_SUB'] = array(
    '#type' => 'textfield',
    '#size' => 30,
    '#title' => t('Subscriber IP Address'),
    '#required' => TRUE,
  );
}

You should add '#tree' => TRUE on your form, it will be easier to extract the form values afterwards

Your Answer

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

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