2

I have a form with submit AJAX button. When clicking this button, it calls function callbackAjax(). In this function callback, I checked a condition A to show a warning dialog if A is FALSE. The warning dialog has a message and two buttons, Confirm and Cancel. Cancel action will close dialog and do nothing. Confirm action will continue doing the callback function with condition A is true.

function renderForm($form, $form_state) {
  $form['submit'] = array(
    '#type'  => 'submit',
    '#ajax'  => array(
      'callback' => 'callbackAjax',
    ),
  );
}

function callbackAjax($form, &$form_state) {
  if (!$condition) {
    // Show dialog with confirm button
    // If confirmed, doing code in else clause
  }
  else {
    // Save data
  }
}

Please tell me how to re-submit form to save data after I clicked button Confirm which be rendered from AJAX callback?

I tried to set button Confirm as same as Submit button from main from, to call AJAX again, but it shows error.

Thanks for your help.

2
  • why don't you do it in validate?
    – GwenM
    Commented Jun 16, 2015 at 12:34
  • Hi Namari, it is not actually a validation. I want to show confirmation dialog with a warning, which does not stop user action with form_set_error. User can decide to continue his action even if the condition is FALSE. The condition checker must not be in javascript.
    – Duc Le
    Commented Jun 17, 2015 at 3:40

1 Answer 1

1

I found the solution, at least it worked in my case. I want to share if someone need it.

function renderForm($form, $form_state) {
  $form['submit'] = array(
    '#type'  => 'submit',
    '#ajax'  => array(
      'callback' => 'callbackAjax',
    ),
  );
}

function callbackAjax($form, &$form_state) {
  if (empty($form['#skip_validate']) && !$condition) {
    // Show dialog with confirm button
    // If confirmed, doing code in else clause
    $confirm_form = drupal_get_form('renderConfirmForm', $form, $form_state);
    $commands[] = ajax_command_open_dialog($confirm_form);
  }
  else {
    // Save data
  }
  ...
}

function renderConfirmForm($form, &$form_state, $main_form, $main_form_state) {
  $form['continue'] = array(
    ...
    '#ajax'  => array(
      'callback' => 'renderConfirmFormCallback',
    ),
  );
  $form['close'] = ...;
  $main_form['#skip_validate'] = TRUE;
  $form['#main_form'] = $main_form;
  $form['#main_form_state'] = $main_form_state;
  return $form;
}

function renderConfirmFormCallback($form, &$form_state) {
  callbackAjax($form['#main_form'], $form['#main_form_state']);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.