We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30912
    • 463 Posts
    Hi Zaphodx, Did you ever get this to work?

    Thanks
      • 3749
      • 24,544 Posts
      This is a more direct method. I did a little work with this and you don't really need the getObject() call:


      <?php
      $user = $hook->getValue('register.user');
      $userGroup = $hook->getValue('register.usergroup');
      // $userGroup = $hook->getValue('usergroup'); //??
      
      $user->joinGroup($userGroup);
      $user->save();
      ?>
      


      The '$usergoup=' line will depend on the name of the field in your form.


      ---------------------------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
      MODX info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 30912
        • 463 Posts
        I'll let you know how i get on.

        Cheers

        Tyr
          • 30912
          • 463 Posts
          So after 3 hours of trying this last night im clearly missing something.

          heres my info:

          Snippet:

          <?php
          $user = $hook->getValue('register.user');
          $userGroup = $hook->getValue('register.product'); //Product is the name of my dropdown
          // $userGroup = $hook->getValue('usergroup'); //??
           
          $user->joinGroup($userGroup);
          $user->save();
          ?>


          Register Call

          [[!Register?
             &submitVar=`registerbtn`
          &activationResourceId=`14`
          &activationEmailtpl=`My_lgnActivationEmailTpl`
          &activationEmailSubject=`Please activate your account`
          &submittedResourceId=`13`
          &activationttl=`10080`
          &postHooks=`UserGroup`
          &activation=`0`
          
          ]]


          and my form

          <div class="register">
            <div class="registerMessage">[[+error.message]]</div>
            <form class="form" action="[[~[[*id]]]]" method="post">
              <input type="hidden" name="nospam:blank" value="" />
              <ul>
              <li>* indicates required fields</li>
                <li>
                  <label for="username">[[%register.username? &namespace=`login` &topic=`register`]]* <span class="error">[[+error.username]]</span> </label>
                  <input type="text" name="username:required:minLength=6" id="username" value="[[+username]]" />
                </li>
                <li>
                  <label for="password">[[%register.password]]* <span class="error">[[+error.password]]</span> </label>
                  <input type="password" name="password:required:minLength=6" id="password" value="[[+password]]" />
                </li>
                <li>
                  <label for="password_confirm">[[%register.password_confirm]]* <span class="error">[[+error.password_confirm]]</span> </label>
                  <input type="password" name="password_confirm:password_confirm=`password`" id="password_confirm" value="[[+password_confirm]]" />
                </li>
                <li>
                  <label for="fullname">Forename* <span class="error">[[+error.fullname]]</span> </label>
                  <input type="text" name="fullname:required" id="fullname" value="[[+fullname]]" />
                </li>
                <li>
                  <label for="surname">Surname* <span class="error">[[+error.surname]]</span> </label>
                  <input type="text" name="surname:required" id="surname" value="[[+surname]]" />
                </li>
                <li>
                  <label for="email">[[%register.email]]* <span class="error">[[+error.email]]</span> </label>
                  <input type="text" name="email:email" id="email" value="[[+email]]" />
                </li>
                <li>
                  <label for="product">Product* <span class="error">[[+error.product]]</span> </label>
                  <select name="product:required" id="product">
                   <option selected="[[+product]]">[[+product]]</option>
                    <option value="Usergroup1">Usergroup1</option>
                   
                    <option value="Usergroup2">Usergroup2</option>
                    <option value="Usergroup3">Usergroup3</option>
                  
                  </select>
                </li>
                <li><label for="newsletter">Newsletter* <span class="error">[[+error.newsletter]]</span> </label>
                 <select name="newsletter:required" id="newsletter">
                    <option>[[+newsletter]]</option>
                    <option value="Yes" selected>Yes</option>
                    <option value="No">No</option>
                    
                  </select>
               
                </li>
              </ul>
              <br class="clear" />
              <div class="form-buttons">
                <input type="submit" name="registerbtn" value="Register" class="button" />
              </div>
            </form>
          </div>
          
            • 1778
            • 659 Posts
            Hi
            Register works more or less like Formit, and acts the same with select, radios, and checkboxes, I suggest you to have a look at http://rtfm.modx.com/display/ADDON/FormIt.Handling+Selects%2C+Checkboxes+and+Radios...
            I Use Register myself and I had to allow users to choose among 4 groups via checkboxes (they can join one or more group at least one), I did a posthook which check wich value is chosen, and for each use joinGroup to add the user to the corrects group(s), and also set some extended fields for the new created user...
            If it can be of any help let me know...
            Cheers
              • 30912
              • 463 Posts
              It really needs to be a drop down, i understand formit, but im terrible at writing hooks (due mainly to a lack of php knowledge)

              Each user can only join one group, as it depends on what product they own.

              Any example of code would be greatly appreciated.
                • 1778
                • 659 Posts
                Hi again,
                try this it should work (you have to have Formit installed too to use the outputmodifier FormitIsSelected)

                in your form replace your <select...> by :
                <select name="product" id="product">           
                            <option value="Usergroup1" [[!+product:FormItIsSelected=`Usergroup1`]] >Usergroup1</option>
                            <option value="Usergroup2" [[!+product:FormItIsSelected=`Usergroup2`]]>Usergroup2</option>
                            <option value="Usergroup3" [[!+product:FormItIsSelected=`Usergroup3`]]>Usergroup3</option>
                </select>
                


                postHook code:
                $user = $hook->getValue('register.user'); // the new registered user
                $group = $hook->getValue('product'); 
                $modx->log(modX::LOG_LEVEL_ERROR, 'group : '.$group); // this line just to return thr value in your error.log
                if(isset($group)){
                    $user->joinGroup($group);
                $modx->log(modX::LOG_LEVEL_ERROR, 'user as joined group : '.$group); // idem
                }
                return true;
                


                You can remove the lines starting with $modx->log(...), it's just for debugging to see if all is fine and where it fails if not..

                Hope this helps
                Cheers
                If you have problems let me know
                  • 30912
                  • 463 Posts
                  You are a genious, works like a charm.

                  *big electronic hug*
                    • 11055 ☆ A M B ☆
                    • 3,112 Posts
                    it's been a while, but Register 1.8.1 (since 8 months ago, actually) can use &usergroupsField property.
                    https://github.com/splittingred/Login/pull/54

                    I've just added it to the doc as well.
                    Looks like that thing was missed.
                      Rico
                      Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                      MODx is great, but knowing how to use it well makes it perfect!

                      www.virtudraft.com

                      Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                      Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                      Maintainter/contributor of Babel

                      Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
                      • 42562
                      • 1,145 Posts
                      But can the &usergroupsField property be manipulated somehow so that the user sees the groups as options from which to choose?
                      If this is possible, then it'll probably beat any other method.
                      So instead of having multiple register calls with specified usergroups attached, a single call is made with drop-down or check-box options.

                      Also, anyone know if the above posthook and
                      <select name="product" id="product">           
                                  <option value="Usergroup1" [[!+product:FormItIsSelected=`Usergroup1`]] >Usergroup1</option>
                                  <option value="Usergroup2" [[!+product:FormItIsSelected=`Usergroup2`]]>Usergroup2</option>
                                  <option value="Usergroup3" [[!+product:FormItIsSelected=`Usergroup3`]]>Usergroup3</option>
                      </select>
                      
                      will work in UpdateProfile, so that a user may change or add self to another group at will?
                        TinymceWrapper: Complete back/frontend content solution.
                        Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
                        5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.