0

I have an ajaxified form which does successfully update the form on the first AJAX call but not the second. When I debug the code, I see that the same AJAX command is returned in the javascript console each time, but only the first call updates the page elements. The only difference in the returned JSON commands is that the form element id's have been incremented by 1.

Here is my code:

Form

$form['clone_container'] = array(
  '#type' => 'fieldset',
  '#prefix' => '<div id="clone-timesheet-wrapper"><label>Clone Past Timesheet</label>',
  '#suffix' => '</div>',
);
$form['clone_container']['past_timesheets'] = array(
  '#type' => 'select',
  '#title' => t('Past Timesheets'),
  '#options' => $past_timesheets,
);
$form['clone_container']['clone_timesheet'] = array(
  '#type' => 'submit',
  '#title' => t('Clone'),
  '#value' => t('Clone'),
  //'#id' => 'edit-clone-timesheet',
  "#ajax" => array(
    'callback' => 'clone_timesheet_callback',
    'wrapper' => 'timesheet-node-form',
    //'wrapper' => 'edit-field-timsheet-tasks',
  ),
  '#executes_submit_callback' => FALSE,
  '#limit_validation_errors' => array(),
);

....
/*
 * Clone Rebuild
 */
 if (isset($form_state['values']['clone_timesheet'])) {
  $form['test_field'] = array(
    '#markup' => 'this is a test.' . time(),
  );

  }

Form Callback

function clone_timesheet_callback($form, $form_state) {
return $form;
}
  • doesthe submit button need a unique name and array key? – Kevin Feb 5 '18 at 17:05
  • @Kevin Can you clarify your question? by name do you mean #name? and what array key? – user1015214 Feb 5 '18 at 17:59
  • The array key, ex $form['clone_container']['clone_timesheet'] – Kevin Feb 5 '18 at 18:34
0

I figured out what was happening. Since I was returning the $form element, and the wrapper was timesheet-node-form, when it was rebuilding the form, it changed the id to timesheet-node-form--1, thus breaking the next AJAX itteration. I decided to add a new wrapper around the entire form and then target that ID, it worked perfectly.

$form['clone_container']['clone_timesheet'] = array(
  '#type' => 'submit',
  '#title' => t('Clone'),
  '#value' => t('Clone'),
  "#ajax" => array(
    'callback' => 'clone_timesheet_callback',
    'wrapper' => 'timesheet-node-form-wrapper',
  ),
  '#executes_submit_callback' => FALSE,
  '#limit_validation_errors' => array(),
);

// Add a wrapper around the form for AJAX targeting.
$form['#prefix'] = '<div id="timesheet-node-form-wrapper">';
$form['#suffix'] = '</div>';

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.