Answers.Body: <p>You can only do this with custom PHP otherwise this is going to be a high security risk. Imagine someone just changes your HTML and inserts a different group id: everyone could just join the group he/she likes.</p>
  
  <p>Instead you can <a href="https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module" rel="nofollow noreferrer">Enhancing a Craft CMS 3 Website with a Custom Module</a> - there is already a default one and insert an event to do this by yourself but you need to be careful. I wouldn't trust user input in that case too much.</p>
  
  <pre class="lang-PHP prettyprint-override"><code>Event::on(
      Elements::class,
      Elements::EVENT_AFTER_SAVE_ELEMENT,
      function(ElementEvent $event){
          if(\get_class($event-&gt;element) === User::class){
              $joinSpecialGroup = (bool)Craft::$app-&gt;getRequest()-&gt;getBodyParam('joinSpecialGroup');
              if($joinSpecialGroup === true){
                  Craft::$app-&gt;getUsers()-&gt;assignUserToGroups($event-&gt;element-&gt;id, [4]);
              }
          }
      }
  );
  </code></pre>
  
  <p>Note: I woulnd't call the property <code>joinSpecialGroup</code> - it is just for demonstration</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-10T12:08:55.967Z
Answers.Text: Thanks for the example. Before others implement - 
  
  To get the from an unrestricted form parameter is hugely unsafe as malicious users would be able to assign themselves to an arbitrary group (i.e. with control panel rights for example)
Answers.UserDisplayName: 24HOURSMEDIA
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-27T21:28:17.130Z
Answers.LastActivityDate: 2018-09-27T21:28:17.130Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <p>We just had to do this and tried Freeform Pro and their Element Connection feature. It took a few minutes to build the whole thing including all the required emails.</p>
  
  <p><a href="https://solspace.com/craft/freeform/docs/user-registration-forms/" rel="nofollow noreferrer">https://solspace.com/craft/freeform/docs/user-registration-forms/</a></p>
  
  <p>Also - just noticed Craftquest has a free tutoarial on building user registration form using Freeform.</p>
  
  <p><a href="https://craftquest.io/courses/managing-forms-with-solspace-freeform/5246" rel="nofollow noreferrer">https://craftquest.io/courses/managing-forms-with-solspace-freeform/5246</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-29T02:06:00.393Z
Answers.LastActivityDate: 2018-09-29T02:13:30.023Z
Answers.LastEditDate: 2018-09-29T02:13:30.023Z
Answers.UserDisplayName: JKM
Body: <p>I'm trying to register a user from the front-end and assign them to a specific group. I know that via control panel I can set a default user group, but there is a registration instance where I need the registrant to be assigned to a different group.</p>
  
  <p>I tried this to no avail...</p>
  
  <pre><code>&lt;input type="hidden" name="groups" value="4"&gt;
  </code></pre>
  
Tags: <craft3><registration>
Title: Register a user to a specific group
ThreadText: # Register a user to a specific group
  
  (Asked by: username_0 on 2018-09-27)
  
  I'm trying to register a user from the front-end and assign them to a specific group. I know that via control panel I can set a default user group, but there is a registration instance where I need the registrant to be assigned to a different group.
  
  I tried this to no avail...
  
  ```
  <input type="hidden" name="groups" value="4">
  
  ```
  
  **Tags:** craft3, registration
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-09-27. Score: 1
  
  You can only do this with custom PHP otherwise this is going to be a high security risk. Imagine someone just changes your HTML and inserts a different group id: everyone could just join the group he/she likes.
  
  Instead you can [Enhancing a Craft CMS 3 Website with a Custom Module](https://nystudio107.com/blog/enhancing-a-craft-cms-3-website-with-a-custom-module) - there is already a default one and insert an event to do this by yourself but you need to be careful. I wouldn't trust user input in that case too much.
  
  ```PHP
  Event::on(
      Elements::class,
      Elements::EVENT_AFTER_SAVE_ELEMENT,
      function(ElementEvent $event){
          if(\get_class($event->element) === User::class){
              $joinSpecialGroup = (bool)Craft::$app->getRequest()->getBodyParam('joinSpecialGroup');
              if($joinSpecialGroup === true){
                  Craft::$app->getUsers()->assignUserToGroups($event->element->id, [4]);
              }
          }
      }
  );
  
  ```
  
  Note: I woulnd't call the property `joinSpecialGroup` - it is just for demonstration
  
  > **Comments:**
  > **username_2**: Thanks for the example. Before others implement -   To get the from an unrestricted form parameter is hugely unsafe as malicious users would be able to assign themselves to an arbitrary group (i.e. with control panel rights for example)  
  
  ---
  
  ### Answer by username_3 on 2018-09-29. Score: 1
  
  We just had to do this and tried Freeform Pro and their Element Connection feature. It took a few minutes to build the whole thing including all the required emails.
  
  <https://solspace.com/craft/freeform/docs/user-registration-forms/>
  
  Also - just noticed Craftquest has a free tutoarial on building user registration form using Freeform.
  
  <https://craftquest.io/courses/managing-forms-with-solspace-freeform/5246>
  
  
  ---
  
  