We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1778
    • 659 Posts
    Hi all
    I'm using Login.Register to register my users... All works fine if the property '&usergroups' is "hard-coded" like that
    [[!Register?
        &submitVar=`registerbtn`
        &activationResourceId=`57`
        &activationEmailTpl=`activationEmailTpl`
        &activationEmailSubject=`Thanks`
        &submittedResourceId=`70`
        &usergroups=`Group1,Group2`
        &validate=`nospam:blank,
            username:required
            password:required,        
            password_confirm:password_confirm=^password^,
            name:required`    
        &placeholderPrefix=`reg.`    
    ]]
    


    Now I need to let my users choose the Group they will be assigned. So I put some checkboxes in my form and then I'm facing 2 problems:

    1/ the values of the checked checkboxes are not 'transmitted' in the hidden field :
    Having a look at the $_POST and I see groups[] groups[]=Group1 groups[]=Group3 (in the case I checked Group1 and Group3)-
    In the email activation [[+groups]] is printed as it is [[+groups]], but if I put [[+groups.0]], [[+groups.1]] [[+groups.2]] the checked values are returned and others return [[+groups.n]]
    2/ I can't find a way to set the property '&usergroups' dynamically with the values of the checkboxes.
    Here the code for the checkboxes
    <input type="hidden" name="groups[]" value="" />
    <input type="checkbox" class="styled" name="groups[]" value="Group1" [[!+reg.groups:isChecked=`Group1`]] > G1 Member</input>
     
    <input type="checkbox" class="styled" name="groups[]" value="Group2" [[!+reg.groups:isChecked=`Group2`]] > G2 Member</input>
     
    <input type="checkbox" class="styled" name="groups[]" value="Group3" [[!+reg.groups:isChecked=`Group3`]] > G3 Member</input>
     
    


    The 'isChecked' modifier is a basic copy/paste of the formitIsChecked snippet.

    INSTALLATION :
    Revolution 2.2.0-rc3 from git
    WAMP server install on Windows7
    PHP 5.3.8
    MySQL server: 5.5.16-log
    MySQL Client : mysqlnd 5.0.8-dev - 20102224 - $Revision: 310735 $

    I'm facing a wall and any help would be appreciated
    Thanks in advance
    [ed. note: anso last edited this post 12 years, 6 months ago.]
    • anso,

      I'm not sure to completely follow how you are doing things here.
      A possible (untested) solution could be a postHook (again!). Since your groups field seems like to be an array, implode(',', $hook->getField('groups')); should return 'Group1,Group3'. Then you should be able to define the Register usergroups parameter ($scriptProperties['usergroups'] = implode(',', $hook->getField('groups'))).

      That's just an idea/how i would try to do it, but once again, i do not have tested this...

      Hope that will help
      (Bonne année à toi également)
        • 1778
        • 659 Posts
        Romain,
        I'm a bit confused myself (tired and sick as I said)...
        The checked values are stored as extended field (in the user profile) so yes it's an array (once the user object is saved) ... so I think the simplest way is to get the values from the Profile and use joinGroup() to add my user to the correct groups...

        I will try too to set the &usergroups property as you suggest using ($scriptProperties['usergroups'] = implode(',', $hook->getField('groups'))).... But if I do that in a postHook the $scriptProperties is already evaluated and set (true ?) ? It cannot be a preHook (i need the checked values), do I have to put it in a validator(on field groups[]) instead ...

        For now I need to have a sleep... Tomorrow with a fresh (almost) brain and clearest ideas (i hope) I suppose I'll achieve this...
        Thanks again for your help

        • anso,

          I've just tested and you seem to be right, there is no way to change the usergroups parameter (at least not with $scriptProperties in a postHook). As you wrote storing the values to the modUserProfile can be an option.

          Another one (food for though) can be to redirect to an "hidden" resource (using &submittedResourceId), storing the groups & user id values (using &persistParams). On the hidden resource you would have your snippet grabbing the parameters set with persistParams, adding the users to the groups and finally a sendRedirect to where you want.

          I'm currently working on an invitation system (working together with Login snippets) and i'm doing likewise to create the relationships ("inviter" > invitee).

          Get well soon wink
            • 1778
            • 659 Posts
            Hi Romain, Hi all,

            After a good sleep things go clearer ! I finally have a working postHook that set the username with name and firstname, and assign user to groups he has selected via checkboxes.
            For those interested here's my code
            <?php
            $username = $hook->getValue('username');
            $name = $hook->getValue('name');
            $firstname = $hook->getValue('firstname');
            
            // set the fullname with name and first name
            $user = $hook->getValue('register.user');
            $profile = $hook->getValue('register.profile');
            $profile->set('fullname', $hook->getValue('name') . ' ' . $hook->getValue('firstname'));
            
            // get the extented fields
            $fields = $profile->get('extended');
            $groups = $fields['group']; // this one is an array containing the checkboxes values
            foreach ($groups as $key=>$value){
                $user->joinGroup($value);
            }
            // save $profile to update fullname, save $user to be sure
            $profile->save();
            $user->save() ;
            return true;
            
            


            Cheers
            • You can have users put into groups with check box input fields like this (Login 1.9.0-pl):
              <input type="checkbox" name="usergroups[]" id="groups1" value="Members" /> Members
              <input type="checkbox" name="usergroups[]" id="groups2" value="Others" /> Others
              


              [[!Register?
                  &submitVar=`registerbtn`
                  &activationResourceId=`11`
                  &activationEmailTpl=`lgnActivateEmailTpl`
                  &activationEmailSubject=`Thanks for Registering!`
                  &submittedResourceId=`10`
                  &usergroupsField=`usergroups`
              ]]
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org