I have a Form which on submit calls another form via ajax and embeds it into a modal popup (Drupal8 standard) and when I click on submit of that modal form , I get a Drupal ajax error (I think it's due to the fact that ajax tries to find the from on the wrong page).

Here is how the submit button of the first form looks:

$form['table'][$pid]['edit'][$cid] = [
      '#type' => 'submit',
      '#id' => 'edit_'.$cid,
      '#name' => 'edit_'.$cid,
      '#value' => $this->t('Edit'),
      '#ajax' => [
        'callback' => [$this, 'customAjaxModalForm'],
        'event' => 'click',

And here's the customAjaxModalForm method, which calls the other form and modal.

public function customAjaxModalForm(array $form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  $modal_form = \Drupal::formBuilder()->getForm('\Drupal\accounts\Form\ContactsForm');    
  $response->addCommand(new OpenModalDialogCommand('Edit contact', $modal_form, ['width' => '800', 'height' => '300']));
  return $response;


Now the submit of the ContactsForm looks exactly as the submit above, as I need to send a few responses to the client (.js).

The rest of the code is pretty straight forward so I only supplied the most crucial parts.

How my code works, is: you go to a page, controller handles the request, it calls the first form (the form only has a textfield and three submit buttons, the other two submit buttons work - do their job) the rest you know.

Any ideas? I am at a loss.

2 Answers 2


Ok, after a a few hours tinkering and extensive googling, i sadly found the answer i was looking for (but not hoping for).

How to open a popup form from a view link?

Turns out, in D8 this is impossible.


i was also faced the same problem . drupal 8 examples shows only the popup when user clicks the link instead of form submission .so changed the popup as alert in javascript for my scenario

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.