0

I've custom form (suppose FORM1) with few fields and AJAX submit button. When user submit this form, I'm opening another form (suppose AJAXFORM1) in ModalDialog with few more fields and AJAX submit button. When I open AJAXFORM1 using link (i.e. a tag with 'use-ajax' class), form submit handler gets call and modal dialog is closing properly. But when I open AJAXFORM1 from submit handler of FORM1, form submit handler of AJAXFORM1 is not called (neither normal submit handler nor ajax callback). Below is code snippet I'm using:

FORM1

class CustomForm extends FormBase {

  /**
   * {@inheritdoc}.
   */
  public function getFormId() {
    return 'custom_form';
  }

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = [];

    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Submit'),
      '#name' => 'submit',
      '#ajax' => [
        'callback' => [$this, 'ajaxFormSubmit'],
      ],
    ];
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';

    return $form;
  }

  public function ajaxFormSubmit(array &$form, FormStateInterface $form_state) {
    $response = new AjaxResponse();

    $options = [
    ];

    $modal_form = \Drupal::formBuilder()->getForm('Drupal\mymodule\Form\ConfirmDialogForm', $options);
    $response->addCommand(new OpenModalDialogCommand('My Modal Form', $modal_form));

    return $response;
  }

}

AJAXFORM1

class ConfirmDialogForm extends FormBase {

  /**
   * {@inheritdoc}.
   */
  public function getFormId() {
    return 'confirm_dialog_form';
  }

  /**
   * {@inheritdoc}.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $options = []) {
    $form = [];

    $form['confirm'] = [
      '#type' => 'submit',
      '#value' => $this->t('Ok'),
      '#ajax' => [
        'callback' => [$this, 'submitModalFormAjax'],
        'event' => 'click',
      ],
    ];

    $form['#cache'] = ['max-age' => 0];
    return $form;
  }

  /**
   * {@inheritdoc}.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    \Drupal::logger('submit')->error('<pre>' . print_r($values, TRUE) . '</pre>');
    // return TRUE;
  }

  public function submitModalFormAjax(array $form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    $triggerd_element = $form_state->getTriggeringElement();
    \Drupal::logger('submit')->error('<pre>' . print_r($values, TRUE) . '</pre>');

    $response = new AjaxResponse();

    $redirect_to = 'redirect.to.route';
    $redirect_url = Url::fromRoute($redirect_to, [], ['absolute' => TRUE])->toString();
    $response->addCommand(new RedirectCommand($redirect_url));

    // $response->addCommand(new CloseDialogCommand());
    $response->addCommand(new CloseModalDialogCommand());

    return $response;
  }

}
2
  • ajaxFormSubmit does not return the original form
    – user21641
    Commented Aug 7, 2018 at 16:37
  • @IvanJaros can u elaborate more on 'ajaxFormSubmit does not return original form'.
    – Yogesh
    Commented Aug 7, 2018 at 17:39

0

Your Answer

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

Browse other questions tagged or ask your own question.