How to set role on the registration form programmatically I'm trying to create multiple registration forms with a role assigned to each.
It seems that my default value for the role is not keeped, but it works with other fields
Here is my code
$form = $this-&gt;entityFormBuilder-&gt;getForm($account, 'mycustomdisplay');
$form['account']['roles']['#default_value'][] = 'mycustomrole';

Thank you.
You need to do this in a form alter hook, not after the form has already been built.
function module_form_user_form_alter(array &amp;$form, FormStateInterface $form_state, $form_id) {
  if ($condition) {
    $form['account']['roles']['#default_value'][] = 'mycustomrole';
  }
}