0

I have been experiencing a problem for the past two days that I can't find an answer too. I have two buttons in a form that use an ajax callback (an example is shown below).

$date_selector_container = FALSE;

// Check to see if the user clicked one of the navigation buttons.  
// If so, then change the date forward or backward by a week.
if (isset($date_selector['#value']['display_date'])) {
  dd($form_state, 'form_state');
  dd($form_state['triggering_element']['#date_selector_direction'], 'triggering element');
  if (isset($form_state['triggering_element']) &&
    isset($form_state['triggering_element']['#date_selector_direction'])) {
    if ($form_state['triggering_element']['#date_selector_direction'] == 'prev') {
      dd("User pushed prev button");
      $display_date = $date_selector['#value']['display_date'] - 604800;
    } else if ($form_state['triggering_element']['#date_selector_direction'] == 'next') {
      dd("User pushed next button");
      $display_date = $date_selector['#value']['display_date'] + 604800;
    }
    $date_selector_container = TRUE;

  }
  dd("new display date = ".$display_date);
  unset($date_selector['#value']['display_date']);
}

// A whole bunch of date calculations happen here...

$prev_button = array(
  '#type' => 'button',
  '#value' => '<<<',
  '#id' => $id . '-prev',
  '#name' => $date_selector['#name'] . '[prev]',
  '#date_selector_direction' => 'prev',
  '#ajax' => array(
    'callback' => 'my_date_picker_ajax',
    'wrapper' => $id . '-date-selector-wrapper',
    'progress' => array(
      'message' => NULL,
      'type' => 'throbber',
    ),
  ),
);

$next_button = array(
  '#type' => 'button',
  '#value' => '>>>',
  '#id' => $id . '-next',
  '#name' => $date_selector['#name'] . '[next]',
  '#date_selector_direction' => 'next',
  '#ajax' => array(
    'callback' => 'my_date_picker_ajax',
    'wrapper' => $id . '-date-selector-wrapper',
    'progress' => array(
      'message' => NULL,
      'type' => 'throbber',
    ),
  ),
);

$date_selector['#rows'] = $rows;
$date_selector['#header'] = $header;
$date_selector['#tree'] = TRUE;
$date_selector['#prefix'] = (isset($date_selector['#prefix']) ? $date_selector['#prefix'] : '') . '<div id="' . $id . '-date-selector-wrapper">';
$date_selector['#suffix'] = '</div>' . (isset($date_selector['#suffix']) ? $date_selector['#suffix'] : '');
$date_selector['#display_date'] = $display_date;
$date_selector['next'] = $next_button;
$date_selector['prev'] = $prev_button;
$date_selector['#hidden_values'] = $hidden_values;
if ($date_selector_container) {
  $form_state['ajax_date_selector_container'] = $date_selector;
}
return $date_selector;

Each button is uniquely labelled but follow the same pattern.

The issue I am having is that when either button is clicked $form_state[triggering_element] is not being set. I can tell from debugging statements that the callback is happening but when I query $form_state there is no triggering_element key. I have reviewed the API, checked the code and Googled for answers but can't find anything wrong. Can anyone possibly tell me what the issue might be. I am going crazy here.

Update:

After digging around some more I looked at the $_POST statement and see the field [_triggering_element_name] being set to the button but for some reason never gets put into $form_state. I also see the submit handler is also called but this time $form_state[triggering_element] is set but has a different button's id in that field (ie a button that wasn't clicked). Perhaps that sheds some light on the issue.

Thanks.

  • Can you post your code for review? – vintorg Jan 9 '13 at 21:22
  • What part of the code would you like? The form creation code is quite lengthy. – user5013 Jan 9 '13 at 21:34
  • Can you strip it down? – vintorg Jan 9 '13 at 21:36
  • I updated the example code with a stripped down version (the best I could). Hopefully it helps, if not, then let me know. Thanks. – user5013 Jan 9 '13 at 21:47
  • Ha.. We need your form builder fx, submit fx, and validate fx... with fx header. – vintorg Jan 9 '13 at 21:49

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.