1

I'm trying to save a node (with the normal node form) via an ajax callback. The first save works. But after that I want to return the same node form again so that the user can update the values and save the node again. But this second save attempt fails. I always get a message like "this form has already been submitted". Are any solutions for this problem? Or shall I write a custom JS menu hook which saves the node programmatically?

Here are the relevant parts of my module

function MYMODULE_form_finding_node_form_alter (&$form, &$form_state){
  $form['#prefix'] = '<div id="wrapper">';
  $form['#suffix'] = '</div>';
  $form['actions']['submit']['#ajax'] = array(
    'callback' => 'MYMODULE_finding_node_form_submit_ajax',
    'wrapper' => 'finding-form-wrapper'
  );
}


function MYMODULE_finding_node_form_submit_ajax($form, &$form_state){
  $form_id = '#' . str_replace('_','-',$form['#form_id']);
  $commands = array();
  $commands[] = ajax_command_remove('.l-messages');
  if(form_get_errors()){
    $commands[] = ajax_command_replace($form_id, '<div id="finding-form-wrapper"></div>');
    $commands[] = ajax_command_replace('#finding-form-wrapper', drupal_render($form));
  }
  else{
    $node = $form_state['node'];
    $form_state = array();
    $form_state['rebuild'] = TRUE;
    $form_state['build_info']['args'] = array($node);
    $form = drupal_build_form('finding_node_form', $form_state);
    $form['#id'] = str_replace("_", "-", $form_id);

    $commands[] = ajax_command_replace($form['#id'], drupal_render($form));
  }
  $commands[] = ajax_command_prepend('.page', '<section class="l-messages row"><div class="columns">' . theme('status_messages') . '</div></section>');
  return array('#type' => 'ajax', '#commands' => $commands);
}
0

So I managed to figure it out myself. It just seems that you need to build up the whole new form state manually and then rebuild the form with that state again. Here's my modified code for the ajax submit handler. But if anyone still has a better solution please post your answer here.

function MYMODULE_finding_node_form_submit_ajax($form, &$form_state){
  $commands = array();
  $commands[] = ajax_command_remove('.l-messages');
  if(form_get_errors()){
    $commands[] = ajax_command_replace(NULL, drupal_render($form));
  }
  else{
    $node = $form_state['node'];
    $new_form_state = array();
    $new_form_state['build_info']['args'][] = $node;
    $new_form_state['no_redirect'] = TRUE;
    $new_form_state['input'] = array();
    $new_form_state['values'] = array();
    $new_form_state['method'] = $form_state['method'];
    $new_form = drupal_rebuild_form('finding_node_form', $new_form_state);

    $commands[] = ajax_command_replace(NULL, drupal_render($new_form));
  }
  $commands[] = ajax_command_prepend('.page', '<section class="l-messages row"><div class="columns">' . theme('status_messages') . '</div></section>');

  return array('#type' => 'ajax', '#commands' => $commands);
}

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.