I have a number of pages in my Drupal 8 application where a template is returned from a controller method, and that template has a number of forms on it. I build the form in the controller method using formBuilder->getForm(), and then I set that output to a theme variable which the template renders. So something like this:

Render Form

$addDocumentForm = \Drupal::formBuilder()->getForm('Drupal\candidate_portal_candidate\Form\AddDocumentForm'); 

Add to theme variable

return array(
    '#theme' => 'candidate_dashboard',
    '#add_document_form' => $addDocumentForm,

Render Form in Twig Template

<div id="addDocForm" class="modal1" style="display:none"><div class="modal-content left"><span onclick='closeModal("addDocForm")' class="closeModal">&times;</span><br/>{{add_document_form}}</div></div>

This works fine, and seemed like an OK way to put multiple forms on one page. However, the problem is when there is an error in validateForm() when I submit one of the forms. Instead of returning to the current page with an error on the page, drupal redirects to a page with only the invalid form on it.

How can I get Drupal to show an error message on the actual page the form is rendered on?


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.