<p>The code I have in my original question works well for me, you can use it for as many forms/usergroups as you needed just keep adding them as conditionals. I will be sticking with what I wrote. Anyone reading this please look at the comments as I found them very useful in better understanding other ways of solving the problem.</p>

CC BY-SA 3.0
2017-02-16T23:13:54.833Z
2017-02-16T23:13:54.833Z
Gareth
<p>There's a couple of problems you're probably running into.</p>

<ol>
<li><p>By default, assigning user permissions (either directly or via groups) requires the the currently logged in user to have the <code>assignUserPermissions</code> permission. It sounds like you're doing this from a front-end request, where I'm guessing there is no authenticated session in place.</p></li>
<li><p>User groups and permissions are applied in Craft <em>after</em> the user has already been saved, so even if you got by #1, your changes would probably be overwritten.  The events you should be listening for are probably <a href="https://craftcms.com/docs/plugins/events-reference#userGroups-onBeforeAssignUserToGroups" rel="nofollow noreferrer">onBeforeAssignUserToGroups</a> and <a href="https://craftcms.com/docs/plugins/events-reference#userGroups-onAssignUserToGroups" rel="nofollow noreferrer">onAssignUserToGroups</a>.</p></li>
</ol>

<p>Of course, you can always write a custom plugin that has a controller action, point your form to it, and do whatever custom user saving logic your situation requires as well.</p>

CC BY-SA 3.0
2017-02-16T18:23:51.397Z
Hi Brad, thank's for the help. I am doing this on a front end form and I have just added the code above in my plugin file. I have it working, I hadn't added the hidden input field `<input type="hidden" name="userGroup" value="partners">` in the correct form file! Dummy points for me :-) From what your saying though should I not be doing it the way I have?
Gareth
CC BY-SA 3.0
2017-02-16T21:17:27.120Z
I think what Brad is referring to is that if you set a Default User Group for public registration, then that will get set after the `saveUser` event has happened. This means it will overwrite the value set using the method in your question. If you leave the Default User Group setting to none it will work as it is. If you want to use the Default User Group setting then you will need to use one of the events that Brad links to.
Steve Rowling
CC BY-SA 3.0
2017-02-16T22:05:21.417Z
@SteveRowling - taking my ramblings and turning into something useful is a gift you have.
Brad Bell
CC BY-SA 3.0
2017-02-16T23:09:31.830Z
@SteveRowling many thanks again for all your help in Slack and here. I think what I have works great for what I need then. Thanks too Brad, I always enjoy your ramblings :-)
Gareth
CC BY-SA 3.0
2017-02-16T18:19:07.667Z
2017-02-16T18:19:07.667Z
Brad Bell
<p>If you're looking for a <strong>Craft 3</strong> solution, check out my answer in this ticket: <a href="https://craftcms.stackexchange.com/a/27926/4433">https://craftcms.stackexchange.com/a/27926/4433</a></p>

CC BY-SA 4.0
2018-10-03T16:35:53.183Z
2018-10-03T16:35:53.183Z
Ryan
<p>I am looking to have two separate front end registration forms for adding users to either the partners user group or the members user group. These have been created in the back end with the following user group handles: members and partners.</p>

<p>I have added the following hidden field to the partners registration:</p>

<pre><code>&lt;input type="hidden" name="userGroup" value="partners"&gt;
</code></pre>

<p>and the following hidden field to the members registration:</p>

<pre><code>&lt;input type="hidden" name="userGroup" value="members"&gt;
</code></pre>

<p>I have then created a plugin with the following code:</p>

<pre><code>public function init() {

   parent::init();

     craft()-&gt;on('users.saveUser', function(Event $event) {

      // only fire if new user
      if ($event-&gt;params['isNewUser']) {

          // retrieve the userModel from the event
          $user = $event-&gt;params['user'];

          // check for presence of 'type' POST field
          $userGroup = craft()-&gt;request-&gt;getPost('userGroup');

          $userGroups = array();

          if ($userGroup == 'partners') {
              $userGroups[] = craft()-&gt;userGroups-&gt;getGroupByHandle('partners')-&gt;id;
          }

          if ($userGroup == 'members') {
              $userGroups[] = craft()-&gt;userGroups-&gt;getGroupByHandle('members')-&gt;id;
          }

          if (count($userGroups))
          {
              // assign the user to the groups
              craft()-&gt;userGroups-&gt;assignUserToGroups($user-&gt;id, $userGroups);
          }
      }
  });
}
</code></pre>

<p>This code is not adding the user to either of the groups but the user is created with no problem.</p>

CC BY-SA 3.0
2017-02-16T11:15:08.930Z
Just wondering as I am not a plugin expert or even anything like that but did you turn on Public registration in Settings > Users > Settings and also turned off email verification so they aren't put on hold?
Justin Dekkers
CC BY-SA 3.0
2017-02-16T11:17:00.683Z
@JustinDekkers thank you, yeah I did set public registration on and they are being registered just not added to the user group.
Gareth
CC BY-SA 3.0
2017-02-16T12:28:24.423Z
Found this: https://github.com/lindseydiloreto/craft-autoassignusergroup
Maybe check in the code or maybe it is exactly what you are looking for.
Justin Dekkers
CC BY-SA 3.0
2017-02-16T12:35:54.640Z
@JustinDekkers thank you again. I have looked at that before and it's not doing quite what I need. I just need to figure out why my code is not assigning users to the correct group.
Gareth
CC BY-SA 3.0
2017-02-16T13:54:17.653Z
@Gareth Have you checked the Craft logs to make sure the post variable is being included with the request?
Steve Rowling
CC BY-SA 3.0
2017-02-16T18:19:38.403Z
@SteveRowling thanks again for helping out, I hadn't set the hidden usergroup in the correct form file, what a plonker! It's working now.
Gareth
<plugin-development><user-groups>
Set a user's group on a registration form using a hidden field
# Set a user's group on a registration form using a hidden field

(Asked by: username_0 on 2017-02-15)

I am looking to have two separate front end registration forms for adding users to either the partners user group or the members user group. These have been created in the back end with the following user group handles: members and partners.
I have added the following hidden field to the partners registration:


and the following hidden field to the members registration:


I have then created a plugin with the following code:
public function init() {

   parent::init();

     craft()->on('users.saveUser', function(Event $event) {

      // only fire if new user
      if ($event->params['isNewUser']) {

          // retrieve the userModel from the event
          $user = $event->params['user'];

          // check for presence of 'type' POST field
          $userGroup = craft()->request->getPost('userGroup');

          $userGroups = array();

          if ($userGroup == 'partners') {
              $userGroups[] = craft()->userGroups->getGroupByHandle('partners')->id;
          }

          if ($userGroup == 'members') {
              $userGroups[] = craft()->userGroups->getGroupByHandle('members')->id;
          }

          if (count($userGroups))
          {
              // assign the user to the groups
              craft()->userGroups->assignUserToGroups($user->id, $userGroups);
          }
      }
  });
}

This code is not adding the user to either of the groups but the user is created with no problem.

**Tags:** plugin-development, user-groups

> **Comments:**
> **username_1**: Just wondering as I am not a plugin expert or even anything like that but did you turn on Public registration in Settings > Users > Settings and also turned off email verification so they aren't put on hold?  
> **username_0**: @JustinDekkers thank you, yeah I did set public registration on and they are being registered just not added to the user group.  
> **username_1**: Found this: https://github.com/lindseydiloreto/craft-autoassignusergroup Maybe check in the code or maybe it is exactly what you are looking for.  
> **username_0**: @JustinDekkers thank you again. I have looked at that before and it's not doing quite what I need. I just need to figure out why my code is not assigning users to the correct group.  
> **username_2**: @username_0 Have you checked the Craft logs to make sure the post variable is being included with the request?  
> **username_0**: @SteveRowling thanks again for helping out, I hadn't set the hidden usergroup in the correct form file, what a plonker! It's working now.  

---

## 3 answers

### Answer by username_0 on 2017-02-16. Score: 0 | Accepted answer

The code I have in my original question works well for me, you can use it for as many forms/usergroups as you needed just keep adding them as conditionals. I will be sticking with what I wrote. Anyone reading this please look at the comments as I found them very useful in better understanding other ways of solving the problem.


---

### Answer by username_3 on 2017-02-16. Score: 2

There's a couple of problems you're probably running into.

1. By default, assigning user permissions (either directly or via groups) requires the the currently logged in user to have the `assignUserPermissions` permission. It sounds like you're doing this from a front-end request, where I'm guessing there is no authenticated session in place.
2. User groups and permissions are applied in Craft *after* the user has already been saved, so even if you got by #1, your changes would probably be overwritten. The events you should be listening for are probably [onBeforeAssignUserToGroups](https://craftcms.com/docs/plugins/events-reference#userGroups-onBeforeAssignUserToGroups) and [onAssignUserToGroups](https://craftcms.com/docs/plugins/events-reference#userGroups-onAssignUserToGroups).

Of course, you can always write a custom plugin that has a controller action, point your form to it, and do whatever custom user saving logic your situation requires as well.

> **Comments:**
> **username_0**: Hi Brad, thank's for the help. I am doing this on a front end form and I have just added the code above in my plugin file. I have it working, I hadn't added the hidden input field `<input type="hidden" name="userGroup" value="partners">` in the correct form file! Dummy points for me :-) From what your saying though should I not be doing it the way I have?  
> **username_2**: I think what Brad is referring to is that if you set a Default User Group for public registration, then that will get set after the `saveUser` event has happened. This means it will overwrite the value set using the method in your question. If you leave the Default User Group setting to none it will work as it is. If you want to use the Default User Group setting then you will need to use one of the events that Brad links to.  
> **username_3**: @SteveRowling - taking my ramblings and turning into something useful is a gift you have.  
> **username_0**: @SteveRowling many thanks again for all your help in Slack and here. I think what I have works great for what I need then. Thanks too Brad, I always enjoy your ramblings :-)  

---

### Answer by username_4 on 2018-10-03. Score: 0

If you're looking for a **Craft 3** solution, check out my answer in this ticket: <https://craftcms.stackexchange.com/a/27926/4433>


---

