1

I am trying to add a date, start time, end time to a form - the user can add as many 'rows' as necessary. The form fields are being added to the form correctly, and the $form_state['input'] is showing the correct values that were input on the form. However, the $form_state['values'] is only showing the last one, e.g. if I add three (bringing the total to 4) it only includes the last value.

Relevant code in the form builder:

$num_dates = !isset($form_state['num_dates']) ? 1 : $form_state['num_dates'];
$form_state['num_dates'] = $num_dates;
$form['date']['#prefix'] = '<div id="form-date-wrapper">';
$form['date']['#suffix'] = '</div><!-- /#form-date-wrapper -->';
for ($i = 1; $i <= $num_dates; $i ++)
{
  $form['date']['calendar_date' . $num_dates] = array(
    '#title' => 'Date',
      '#type' => 'textfield',
      '#maxlength' => 15,
      '#attributes' => array('class' => array('calendar-date')),
      '#prefix' => '<div class="form-date-entry-wrapper form-row form-row-single-item">',
  );
  $form['date']['event_start_time' . $num_dates] = array(
  '#title' => 'Start Time',
      '#type' => 'select',
      '#options' => drupal_map_assoc(array(t('9:00 AM'), t('9:15 AM'))),
      '#attributes' => array('class' => array('calendar-date-times')),
  );
  $form['date']['event_end_time' . $num_dates] = array(
  '#title' => 'End Time',
      '#type' => 'select',
      '#options' => drupal_map_assoc(array(t('9:00 AM'), t('9:15 AM'))),
      '#suffix' => '</div> <!-- /form-date-entry-wrapper -->',
      '#attributes' => array('class' => array('calendar-date-times')),
  );
}
$form['add_another_date_time'] = array(
  '#type' => 'submit',
    '#title' => 'Add Another Date/Time',
    '#value' => 'Add Another Date/Time',
    '#limit_validation_errors' => array(),
    '#attributes' => array('class' => array('look-like-text-button')),
    '#submit' => array('exhibitorevents_form_add_date_time_submit'),
    '#ajax' => array(
      'callback' => 'exhibitorevents_form_add_date_time',
        'wrapper' => 'form-date-wrapper',
        'method' => 'append',
        'effect' => 'fade',
    ),
);
    $form['submit'] = array(
  '#title' => 'Submit',
  '#type' => 'submit',
  '#value' => 'submit',
  '#weight' => 100,
);

And the callback functions:

/**
 * Ajax callback for add more dates/times
 */
function exhibitorevents_form_add_date_time($form, $form_state) {
  return $form['date'];
}
/**
 * Submit handler for add more dates/times button
 * 
 * Increments num_dates +1
 */
 function exhibitorevents_form_add_date_time_submit($form, &$form_state) {
   $form_state['num_dates']++;  
   $form_state['rebuild'] = TRUE;
 }

I have added the $form_state['input'] & $form_state['values'] array here: http://pastebin.com/nL3DWHm8 for reference. This particular example, I added one date 'row', making 2 total.

  • where are you trying to get the value. could you show the that function as well. – Mohammed Shameem Jan 21 '13 at 16:29
  • As of now, for testing, I'm just dumping the $form_state on form submission: function giveaway_form_submit(&$form, &$form_state) { _dvd($form_state); ... } – Chris Rockwell Jan 21 '13 at 16:32
  • _dvd() is just a convenience wrapper I use for debugging, btw – Chris Rockwell Jan 21 '13 at 16:35
  • and you have another button that you click to get it – Mohammed Shameem Jan 21 '13 at 16:42
  • 1
    have you added $form['#tree'] = TRUE; ? – Mohammed Shameem Jan 21 '13 at 16:54
1

You should add the $form['#tree'] so that all the values are passed.

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.