0

First of I apologize if this has been asked before, but I haven't found anything that actually does what I want to do.

Question: I'm trying to deliver some ajax commands on after a successful ajax submission of a form.

The Code

function my_module_form_form($form, &$form_state){
    $node = $form_state['build_info']['args'][0];
    $form = array(
        'track_id' => array(
            '#type' => 'textfield',
            '#title' => t('Track ID'),
            '#required' => true,
            '#prefix' => '<div id="form-finished-wrapper-'.$node->nid.'">',
        ),
        'courier' =>array(
            '#type' => 'textfield',
            '#title' => t('Courier Name'),
            '#required' => true,
        ),
        'actions' => array(
            '#type' => 'actions',
             'submit' => array(
                '#type' => 'submit',
                '#ajax' => array(
                    'callback' => 'my_module_form_ajax_submit',
                    'wrapper' => 'form-finished-wrapper-'.$node->nid,
                ),
                '#prefix' => '<div class="modal-footer">',
                '#suffix' => '</div>',
                '#value' => t('OK'),
            ),
        ),
    );

    return $form;
}
function my_module_form_validate($form, &$form_state){
    // form_set_error('courier', 'catch all');
}

function my_module_form_ajax_submit($form, &$form_state){
    $return_form = array(
        'track_id' => $form['track_id'],
        'courier' => $form['courier'],
    );
    return $return_form;
}
function my_module_form_submit($form, &$form_state){

    $node = $form_state['build_info']['args'][0];
    $track_id = $form_state['values']['track_id'];

    $node->field_track_id[LANGUAGE_NONE][0]['value'] = $track_id;
    $node->field_courier_name[LANGUAGE_NONE][0]['value'] = $courier;
    node_save($node);

/* How can I deliver the below commands?

    $ajax_commands = array();
    $ajax_commands[] = ajax_command_invoke('myModal, 'modal', array('hide'));
    $ajax_commands[] = ajax_command_replace('form-finished-wrapper-'.$node->nid, t('Submited Successfuly'));
    ajax_deliver(array('#type' => 'ajax', '#commands' => $ajax_commands));

*/

}

Context

The form appears in a modal my_module_form_ajax_submit is responsible to display validation errors along with messages. I have tried ajax_delivery and ajax_render to return the commands. With ajax_render nothing happens, but with ajax_deliver I get an ajax popup with the commands in JSON. I think I'm close but surely I'm missing something.

Any help appreciated!

  • I have found this question drupal.stackexchange.com/questions/38190/… which is trying to do something close to what I'm trying to do. I get the ~same error. I'm look for an answer that can be done from the form API though, as the answer suggested there won't work in my case – Mike Chris Jul 25 '17 at 13:14
1

You return the ajax commands in your ajax callback function.

function my_module_form_ajax_submit($form, &$form_state){
  if (count(form_get_errors()) {
    return array(
      'track_id' => $form['track_id'],
      'courier' => $form['courier'],
    );
  }
  else {
    $ajax_commands = array();
    $ajax_commands[] = ajax_command_invoke('myModal', 'modal', array('hide'));
    $ajax_commands[] = ajax_command_replace('form-finished-wrapper-'.$node->nid, t('Submited Successfuly'));
    return array('#type' => 'ajax', '#commands' => $ajax_commands);
  }
}

I'm not sure what you were trying to do with your original callback, so I couldn't incorporate it.

  • the ajax_submit runs every time the button is clicked. I only want to return ajax_commands when the form is successfully submitted. Also the ajax_submit is responsible to show the errors of the form without a page reload ex if you submit without filling in the form it will display that the fields are required. without the original callback these messages would appear on a page refresh. – Mike Chris Jul 25 '17 at 13:01
  • I see. I've altered the code accordingly. – Jaypan Jul 25 '17 at 13:05
  • I've tried something similar, I'll have a go with this and update accordingly, thanks – Mike Chris Jul 25 '17 at 13:19
  • This works perfectly, unfortunately it's my commands that don't work (trying to close the modal and replace the form with text) but any other commands work. As you have answered the context of the question I'm marking this as the right answer. thanks – Mike Chris Jul 25 '17 at 14:08

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.