Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-16T23:13:54.833Z
Answers.LastActivityDate: 2017-02-16T23:13:54.833Z
Answers.UserDisplayName: Gareth
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-16T18:23:51.397Z
Answers.Text: 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?
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-16T21:17:27.120Z
Answers.Text: 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.
Answers.UserDisplayName: Steve Rowling
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-16T22:05:21.417Z
Answers.Text: @SteveRowling - taking my ramblings and turning into something useful is a gift you have.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-16T23:09:31.830Z
Answers.Text: @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 :-)
Answers.UserDisplayName: Gareth
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-02-16T18:19:07.667Z
Answers.LastActivityDate: 2017-02-16T18:19:07.667Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-10-03T16:35:53.183Z
Answers.LastActivityDate: 2018-10-03T16:35:53.183Z
Answers.UserDisplayName: Ryan
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-02-16T11:15:08.930Z
Comments.Text: 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?
Comments.UserDisplayName: Justin Dekkers
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-02-16T11:17:00.683Z
Comments.Text: @JustinDekkers thank you, yeah I did set public registration on and they are being registered just not added to the user group.
Comments.UserDisplayName: Gareth
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-02-16T12:28:24.423Z
Comments.Text: Found this: https://github.com/lindseydiloreto/craft-autoassignusergroup
  Maybe check in the code or maybe it is exactly what you are looking for.
Comments.UserDisplayName: Justin Dekkers
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-02-16T12:35:54.640Z
Comments.Text: @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.
Comments.UserDisplayName: Gareth
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-02-16T13:54:17.653Z
Comments.Text: @Gareth Have you checked the Craft logs to make sure the post variable is being included with the request?
Comments.UserDisplayName: Steve Rowling
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-02-16T18:19:38.403Z
Comments.Text: @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.
Comments.UserDisplayName: Gareth
Tags: <plugin-development><user-groups>
Title: Set a user's group on a registration form using a hidden field
ThreadText: # 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:
  
  ```
  <input type="hidden" name="userGroup" value="partners">
  
  ```
  
  and the following hidden field to the members registration:
  
  ```
  <input type="hidden" name="userGroup" value="members">
  
  ```
  
  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**: @username_1 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**: @username_1 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**: @username_2 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 username_3, 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 username_3 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 username_3 links to.  
  > **username_3**: @username_2 - taking my ramblings and turning into something useful is a gift you have.  
  > **username_0**: @username_2 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 username_3, 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>
  
  
  ---
  
  