Conditional user fields

Use case

We’re working on improving the onboarding experience of our community. Naturally, we’d like to use information we know about the user to do/show certain things based on the information they give us.

Today, our users start their account creation through our OIDC integration by creating an account with our company. They provide email, name, and username to OIDC before it kicks them back over to Discourse.

The problem

We’d like to now give them some additional user fields to fill out—which is great, because Discourse offers this! Unfortunately we can’t show fields based on the responses of previous fields. For example, we’d like to ask, “What role best describes you?” with the options, developer, administrator, and analyst.

  • If they choose developer, I of course want to ask them what their preferred programming languages are. I will also later add them to the developer user group.
  • If they choose analyst, I want to ask them a different series of questions, and add them to a different user group.

Proposed solution

Many form options across the web today offer conditional form fields (e.g. where did you hear about us? if ‘other’ is selected, an additional field shows up to say why.). It would be great if you started down a similar (yet more robust) path as you have with new topic form templates, though with the added requirement of the ability to depend on another field ID, and specifically that field’s input. As an example, it might look something like this:

- type: dropdown
  id: user-type
  choices:
    - "Developer"
    - "Application Administrator"
    - "Analyst"
  attributes:
    none_label: "Choose your role..."
    label: "Which option best describes your job"
  validations:
    required: true
- type: dropdown
  id: developer-type-programming-language
  depends: user-type
    choices:
      - "Developer"
  choices:
    - "Developer"
    - "Application Administrator"
    - "Analyst"
  attributes:
    none_label: "Preferred programming language..."
    label: "What is the primary programming language used in your role?"
  validations:
    required: true

In this example, all users would be asked the first question. If the user selects Developer from the drop-down list, then a second question would appear on the form asking what their preferred programming language is.

This conditional field improvement could be used in both the user onboarding process as well as the original intended place of new topics as well.

5 Likes

You can have a look at GitHub - discourse/discourse-authentication-validations which does just that, except only for the user sign up page. It doesn’t appear trivial to adopt it to the user preferences page where those fields are edited.

But you’re asking for a feature, not looking for a solution to your immediate problem.

6 Likes

Sounds promising—would love to see more documentation around it.

For more information, please see: url to meta topic

Looks like the intention was there.

1 Like

That’s all there is, I’m afraid.

That’s boilerplate from GitHub - discourse/discourse-plugin-skeleton: Template for Discourse plugins

2 Likes

The plugin is actually pretty intuitive.

The bottom checkbox adds the authentication option.

Then populate the trigger responses that will activate the next user custom fields. In my case it is number of headsets greater than None.

Once populated then target the custom user fields you want to show. In my example if 1-6+ then the user will be prompted to put primary and secondary hmds.

This would be great if extended to the experimental Form-templates.

5 Likes

It has been on my todo list for a little while… I’ll be sure to add it this week :slight_smile:

3 Likes

I thought I had wrote this up already… :sweat_smile:

I will update the repo with the meta link.

1 Like

Doh! Sorry I’d forgotten that too.

Will it be possible to have those conditions work in the user profile rather than only on the create account page? (I’m pretty sure that I understand that it’s only working on the account creation page–I have someone who wants to have people go back and enter stuff and have those rules enforced.)

Yes, this is currently the only location affected by this plugin.

Let me make sure we are on the same page:

Are you wanting the ability to update public custom user fields via /preferences/profile and we apply the same conditional hide/show logic as we do on the create an account modal?

Something like, if value of custom field 1 is show custom field 2, then show the second custom field?

2 Likes

Yes. I want all custom_user_fields that are Editable after signup to have those same rules applied on the /preferences/profile page.

I think that’s right. The same rules that are in effect on the create account modal would be in effect on the profile page.

3 Likes

I think this would be a great addition. Moving the conditional field logic to the profile page should be pretty easy.

This would be a good candidate for pr-welcome, but I can also try to take a look in the next month or so.

2 Likes

Well, for you, maybe! I gave it a shot and quickly got lost. It seemed like it should be almost as easy as copying some stuff to the preferences page, but just which stuff was more than I could quickly make sense of.

That would be great!

4 Likes