How do I submit a user registration form in code?
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.