I created 2 forms:
dms_gift_voucher/src/Form/GiftVoucherForm.php
... /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { .... $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Submit'), '#attributes' => [ 'class' => [ 'use-ajax', ], ], '#ajax' => [ 'callback' => [$this, 'confirmGiftVoucher'], 'event' => 'click', ], ]; // Dialog box dependency $form['#attached']['library'][] = 'core/drupal.dialog.ajax'; return $form; } ... /** * Callback for opening the modal form. */ public function confirmGiftVoucher(array $form, FormStateInterface $form_state) { $response = new AjaxResponse(); // Get the modal form using the form builder. $modal_form = $this->formBuilder->getForm('Drupal\dms_gift_voucher\Form\GiftVoucherConfirmForm', $form_state->getValues()); // Add an AJAX command to open a modal dialog with the form as the content. $dialog_options = [ 'width' => 'calc(100% - 20px)', 'dialogClass' => 'modal--gift-voucher-confirm', ]; $response->addCommand(new OpenModalDialogCommand($this->t('Test'), $modal_form, $dialog_options)); return $response; } dms_gift_voucher/src/Form/GiftVoucherConfirmForm.php
... /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state, $values = []) { ... $form['submit'] = [ '#type' => 'submit', '#value' => $this->t('Confirm'), '#weight' => 100, '#attributes' => [ 'class' => [ 'use-ajax', ], ], '#ajax' => [ 'callback' => [$this, 'submitModalFormAjax'], 'event' => 'click', ], ]; // Library $form['#attached']['library'][] = 'core/drupal.dialog.ajax'; return $form; } ... /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Never called! } /** * {@inheritdoc} */ public function submitModalFormAjax(array &$form, FormStateInterface $form_state) { $response = new AjaxResponse(); // Never called! return $response; } The first form works correct, the submit is called, the ajax callback renders the modal with the 2nd form. But when i submit the 2nd form, nothing happens, no submit, no ajax callback called from the 2nd form.
How can i fix this? Any suggestions.
(Every example of drupal modal forms start with a link trigger with routing, but i find it strange why this approach doesn't work).
EDIT: As suggested in the comments, routing info:
dms_gift_voucher.routing.yml
dms_gift_voucher.gift_voucher_form: path: '/dms_gift_voucher/form/gift_voucher' defaults: _form: '\Drupal\dms_gift_voucher\Form\GiftVoucherForm' _title: 'GiftVoucherForm' requirements: _access: 'TRUE' dms_gift_voucher.gift_voucher_cofirm_form: path: '/dms_gift_voucher/form/gift_voucher_cofirm_form' defaults: _form: '\Drupal\dms_gift_voucher\Form\GiftVoucherConfirmForm' _title: 'GiftVoucherConfirmForm' requirements: _access: 'TRUE'