Set a user's group on a registration form using a hidden field 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:

&lt;input type="hidden" name="userGroup" value="partners"&gt;


and the following hidden field to the members registration:

&lt;input type="hidden" name="userGroup" value="members"&gt;


I have then created a plugin with the following 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);
          }
      }
  });
}


This code is not adding the user to either of the groups but the user is created with no problem.
There's a couple of problems you're probably running into.


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.
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 and 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.