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('DrupalaccountsFormContactsForm'); $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.