How to alter $form_state before render a form? I want to render a Custom Form from a Controller class. So i've written the code like this

class MyController extends ControllerBase {

  public function content() {

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


but i want to put some values in $form_state i.e. set #default_values.But here $form is a renderable array, not an object so there is no $form_state object. So, what to set default values ?
An edit form for what exactly?

getForm() accepts arguments. All additional arguments are passed to the buildForm() method of your form. So you pass whatever you are editing in, and when building the form, you get the current value and set it as #default_value of the form elements.

If you are editing entities, then you should consider using an entity form. I'm maintaining the _entityform_block module, which has the following build() method in it's block, you could do something similar in a controller:

$values = array();
// Specify selected bundle if the entity has bundles.
if (\Drupal::entityTypeManager()-&gt;getDefinition($this-&gt;configuration['entity_type'])-&gt;hasKey('bundle')) {
  $bundle_key = \Drupal::entityTypeManager()-&gt;getDefinition($this-&gt;configuration['entity_type'])-&gt;getKey('bundle');
  $values = array($bundle_key =&gt; $this-&gt;configuration['bundle']);
}

$entity = \Drupal::entityTypeManager()
  -&gt;getStorage($this-&gt;configuration['entity_type'])
  -&gt;create($values);

if ($entity instanceof EntityOwnerInterface) {
  $entity-&gt;setOwnerId(\Drupal::currentUser()-&gt;id());
}

$form = \Drupal::entityTypeManager()
  -&gt;getFormObject($this-&gt;configuration['entity_type'], 'default')
  -&gt;setEntity($entity);
return \Drupal::formBuilder()-&gt;getForm($form);