How do I submit a user registration form in code? I have tried to save registration data in a controller.

$values = \Drupal::request()-&gt;request-&gt;all();

     $formBuilder = \Drupal::service('entity.form_builder');
     $form = $formBuilder-&gt;getForm(User::create(array()), 'register', array());
    $formObject = new RegisterForm(\Drupal::entityManager(),
                    \Drupal::languageManager(), \Drupal::service('entity.query'));

     $form_state = (new FormState())-&gt;setValues($values);
                //$formObject-&gt;form($form,$form_state);
     $entity = $formObject-&gt;buildEntity($form,$form_state);
     $formObject-&gt;setEntity($entity);
     $formObject-&gt;form($form,$form_state);
     $formObject-&gt;submitForm($form,$form_state);
     $formObject-&gt;save($form,$form_state);


Above code produces this error: 


  Fatal error: __clone method called on non-object in /home/vagrant/www/drupal8/drupal-8.0.1/core/lib/Drupal/Core/Entity/EntityForm.php on line


It seems I need to inject an "entity" before buildEntity().

How did Drupal 8 manage to create a new user? Maybe the container injected that entity some where, but I am unable to find it.
if you just want to create new user, it is easy as you create the user object and fill it and save it:

$user = \Drupal\user\Entity\User::create();
$user-&gt;setPassword($values-&gt;getValue('pass'));
$user-&gt;enforceIsNew();
$user-&gt;setEmail($values-&gt;getValue('mail'));
$user-&gt;setUsername($values-&gt;getValue('name'));
$user-&gt;activate();
$user-&gt;save();


but in my case for example I wanted drupal to do other stuff like send welcome email...etc, so what i did, instead of last step ( $user->save() ):

$formObject = new RegisterForm($this-&gt;entityManager, $this-&gt;languageManager, $this-&gt;entityQuery);
$formObject-&gt;setEntity($user);
$formObject-&gt;save($form,$form_state); // $form and $form_state can be created like you did.
// if you want to get the newly created user
$user = $formObject-&gt;getEntity();


on the other hand I have tried to get your code working, and the following was the outcome code:

$formObject = $this-&gt;entityManager-&gt;getFormObject('user','register');
$formObject-&gt;setEntity($user);
$formStateObject = (new FormState())-&gt;setFormObject($formObject);
$form = [];
$form = $formObject-&gt;buildForm($form,$formStateObject);
$formObject-&gt;validateForm($form,$formStateObject);
$formObject-&gt;submitForm($form,$formStateObject);
$formObject-&gt;save($form,$formStateObject);


Hope that helps.