Load form and use custom submit handler In my Controller I'm loading a form with the following code.

$form = \Drupal::formBuilder()-&gt;getForm('Drupal\custom_module\Form\CustomForm');


Depending on different cases I would like to use different processing of the form.

I see 3 possibilities to handle this:


Use different forms and load the correct depending on the case
Include some hidden fields and evaluate these fields in the submit handler of the form
Use different submit handlers


I think the third possibility is the best one. But when I try to use a custom submit hanlders.

$form['#submit'] = 'custom_module_form_submit';
// or
$form['actions']['submit']['#submit'][] = 'custom_module_form_submit';


They both don't work. How can this be achieved? Is there a better way of doing this?
@kiamlaluno is right, you have to use the form builder directly. But for this question in the comments there is a solution:


  ... its a custom class extending FormBase. But I still don't get how
  to change the submit handler. In my controller ...


you can put additional parameters in formBuilder()-&gt;getForm('Drupal\custom_module\Form\CustomForm', $param), which you then can use in the form class to build the form differently, for example add different submit handlers:

  public function buildForm(array $form, FormStateInterface $form_state, $param = NULL) {

    if ($param == 1) {
      $form['actions']['submit']['#submit'][] = 'custom_module_form_submit';
    }

  }