Route callback for RegisterForm results in Call to a member function getEntityTypeId() on null
I believe the problem is here:

_form: 'Drupal\mymodule\Form\RegisterByRole'


You need to use _entity_form rather than _entity, as the RegisterForm is actually an entity form for the User entity, as described in the Entity declaration for \Drupal\user\Entity\User:

"form" = {
  ...
  "register" = "Drupal\user\RegisterForm"
},


But to do this, you'll have to register a new form op for that class. You can do this in hook_entity_type_alter():

function hook_entity_type_alter(array &amp;$entity_types) {
  $user = &amp;$entity_types['user'];
  $user-&gt;setFormClass('role_registration', 'Drupal\example\Form\RegisterByRole');
}


Then, back in your route, you'll remove _form and instead implement_entity_form, setting it to to user.role_registration:

_entity_form: 'user.role_registration'