2

Is there a way to allow a registrant to choose a user group in the registration form?

I tried using the following code but it didn't work:

<input type="hidden" name="userGroups" value="3">
Hasco
  • 171
  • 1
  • 9
  • If you're looking for a Craft 3 solution, check out my answer in this ticket: https://craftcms.stackexchange.com/a/27926/4433 – Ryan Oct 03 '18 at 16:39

1 Answers1

3

As far as I know there is no way to do this without a plugin. However, developing a plugin for this is not difficult. In your form you would want to add an additional drop-down, multi-select, or checkbox field with your groups listed. Then in your plugin, create a listener in your plugin's init method that listens for the users.saveUser event, and dynamically assigns the user to the selected groups based on the user's selection. For an example see this post explaining how to respond to an onSaveUser event, and this post on assigning groups dynamically.

Douglas McDonald
  • 13,457
  • 24
  • 57
  • Thanks for your answer. I was wondering apart from this if it's possible to just assign a user to a user group from the front-end registration form? This way I could just build multiple registration forms, each assigning users to their own group. – Hasco Nov 11 '14 at 10:14
  • Unfortunately no. Without the aid of a plugin, its only possible to define a single default group for all public registrations. – Douglas McDonald Nov 11 '14 at 14:41
  • I thought I should add: for securities sake it would be best not to just allow any group ID to be assigned (i.e. don't just assign the group id that is passed from the form blindly), but rather selectively assign the groups that you do want to allow. Otherwise an attacker might be able to make themselves admin. – Douglas McDonald Nov 11 '14 at 16:13
  • I agree, so the attempt from my question is definitely a no go. In the meantime I wrote a plugin to solve my issue. Users are now able to select a group they want to sign up for in the registration form. I'll post it here as an answer. – Hasco Nov 11 '14 at 17:32
  • What you have above could work, just check (i.e. if 1 then group 1; if 2 then group 2; if 3 then redirect to department of homeland security cyberattack division) :) – Douglas McDonald Nov 11 '14 at 21:26
  • Haha, what if I made 3 different forms which all have their own assigned user group, would that be more secure? – Hasco Nov 11 '14 at 22:00
  • One form is fine. Just restrict what user groups they can be assigned to and your fine. – Douglas McDonald Nov 12 '14 at 01:04