Extra field in phx.gen.auth's UserRegistrationLive not under "user"

I am adjusting the user_registration_live.ex generated by phx.gen.auth --live to include a couple of extra fields.

Neither of them is nested; they are just placed right above and below the inputs for email and password in the simple_form component:

        <.input
        field={@form[:preferred_role]}
        type="text"
        name="preferred_role"
        label="Primary role"
        value={"#{if not is_nil(@params.role), do: String.capitalize(@params.role)}"}
        required
      />

        <.input field={@form[:email]} type="email" label="Email" required />
        <.input field={@form[:password]} type="password" label="Password" required />

        <.input
          field={@form[:accept_terms]}
          type="checkbox"
          label="I accept the Terms and Conditions"
          required
        />

In the debug output of HANDLE EVENT "save" when I submit the form, I can see that one of those fields ends up in the “user” map, while the other one in the parameters, i.e. at the same level as _csrf_token:

[debug] Replied in 8ms
[debug] HANDLE EVENT "save" in LoginflowWeb.UserRegistrationLive
  Parameters: %{"_csrf_token" => "e1MFPD9SWR0wDXlPDnYvHgQ4N1UjODN2-jLEq4nWcu5xD4cgguVdDhy5", "preferred_role" => "Supplier", "user" => %{"accept_terms" => "true", "email" => "user@example.com", "password" => "[FILTERED]"}}
[debug] Replied in 3ms

What determines this very weird behavior?

Is because you’re mixing field with name and value. The former takes care of the latter two. It’s coming through the top level because you called it simply preferred_role instead of user[preferred _role]. That said I would just use field and do the capitalization stuff in the changes or with JS otherwise it won’t be change tracked.

Thank you, this works!

I removed the name attribute, but left value as-is to pre-fill this, plus added the readonly attribute to the input component, as its value comes from the assigns, from a LiveComponent in the previous step of the flow.

I need to have the text generated there, because eventually I want to localize its value, as it’s actually one of various options in an Enum.

Thanks again!