Every form submission throws EnforcedResponseException

Created on 18 August 2020, over 4 years ago
Updated 15 October 2024, 6 months ago

I created a custom controller where I insert a form:

my.routing.yml:

aw_candidacy_check.politician.attendance_form:
  path: '/cc/{parliament_period}'
  defaults:
    _controller: '\Drupal\aw_candidacy_check\Controller\PoliticianAttendance::build'
  requirements:
    _custom_access: '\Drupal\aw_candidacy_check\AccessHandler\PoliticianAttendanceAccess::access'

In the controller the form is rendered in this way:

\Drupal::formBuilder()->getForm('\Drupal\aw_candidacy_check\Form\PositionsForm', $this->candidacyCheck, $this->attendance);

The arguments are derived from the given parliament_period routing parameter. The form's buildForm method looks like:

public function buildForm(array $form, FormStateInterface $form_state, CandidacyCheck $candidacy_check = NULL, CCAttendance $attendance = NULL) {
    $form['actions'] = [
      '#type' => 'actions'
    ];

    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => 'Speichern'
    ];

    return $form;

The submitForm method does nothing. Every time I submit this form I get an exception thrown in FormBuilder->buildForm() in line 352:

    if ($response instanceof Response) {
      throw new EnforcedResponseException($response);
    }

Somewhere on the journey the $response is created and it is correctly the path of my controller. Why is this exception thrown? What do I miss or what is wrong with my controller / form class?

💬 Support request
Status

Closed: outdated

Version

9.3

Component

forms system

Created by

🇩🇪Germany tobiberlin Berlin, Germany

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇧🇷Brazil paulocs Belo Horizonte

    I had exactly the same issue.
    I was not able to add the $this->formBuilder->getForm() in the try catch block of my controller.
    I moved it out from there (not the best solution), as I needed the redirect to keep working.

Production build 0.71.5 2024