How to pass variables to OpenModalDialogCommand Drupal 8.
Clicking the upload button on Form1 should open UploadForm in a modal dialog.

/**
 * Callback to display a modal.
 */
public function ajaxSubmit(&amp;$form, FormStateInterface $form_state) {
  $op = $form_state-&gt;getTriggeringElement()['#return_value'];
  switch ($op) {
    case 'upload':
      $modal = new OpenModalDialogCommand(
        t('Upload file(s)'), 
          render(\Drupal::formBuilder()-&gt;getForm('UploadForm', 
            $this-&gt;nid, $this-&gt;foo, $this-&gt;bar))));
      break;

    // ... other cases.
  }

  $response = new AjaxResponse();
  $response-&gt;setAttachments($form['#attached']);
  $response-&gt;addCommand($modal);
  return $response;
}


I'm not able to pass the variables to UploadForm. $this-&gt; properties don't appear in UploadForm.

\Drupal::formBuilder()-&gt;getForm('UploadForm', $this-&gt;nid, $this-&gt;foo, $this-&gt;bar));


The form also fails if I replace the variables by local variables 

$nid, $foo, $bar


But it works when I replace them by

'28', 'foo', 'bar'


The non-ajax version of the form uses a redirect to a form route (my_module.uploadform) and works OK.
Is there a way to pass the parameters to UploadForm?
Solved.
Thanks @kiamlaluno.
I was convinced the variables were OK. But after your comment and a fresh look I saw what I was missing:
$this-&gt;nid, $this-&gt;foo, $this-&gt;bar are optional parameters for the UploadForm formbuilder. I was passing only nid and bar, but missing foo, which messed-up things.
So, instead of:

\Drupal::formBuilder()-&gt;getForm('UploadForm', $this-&gt;nid, $this-&gt;bar) //don't need foo


I needed to call:

\Drupal::formBuilder()-&gt;getForm('UploadForm', $this-&gt;nid, null, $this-&gt;bar)


A bit obvious, I'm afraid...