We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1778
    • 659 Posts
    Hello
    Following my previous post on Register, I'm facing another problem now...

    I have 2 input fields 'name' and 'firstname'

    I would like to set the user fullname with the name and the firstname values. Ok seems to be a work for a postHook so I wrote this one
    $username = $hook->getValue('username');
    if (empty($username)) {
        $hook->addError('user', 'Username field empty');
        return false;
    }
    
    $name = $hook->getValue('name');
    if (empty($name)) {
        $hook->addError('user', 'name field empty');
        return false;
    }
    
    $firstname = $hook->getValue('firstname');
    if (empty($firstname)) {
        $hook->addError('user', 'firstname field empty');
        return false;
    }
    
    $usrobject = $modx->getObject('modUser', array('username' => $username));
    $userid = $usrobject->get('id');
    if (empty($userid)) {
        $hook->addError('user', 'Error getting user object');
        return false;
    }
    $usrobject->set('fullname', $name . ' ' . $firstname);
    $usrobject->save();
    
    


    But it doesn't work, no error in the modx log, the form is processed, the user is saved, the email activation is well sent, but the user fullname still desperatly empty...

    Any help would be appreciated (I've read all the forum post, and try many of the solutions (or part of) given, read an re-read the rtfm docs Login and Formit) and can't find a way to achieve this...

    Thx [ed. note: anso last edited this post 12 years, 3 months ago.]
      • 28215
      • 4,149 Posts
      Post your Register call.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 1778
        • 659 Posts
        Hello Shaun,
        long time no see... Happy new year ^_^
        Here's the Register call:
        [[!Register?
            &submitVar=`registerbtn`
            &activationResourceId=`57`
            &activationEmailTpl=`activationEmailTpl`
            &activationEmailSubject=`Thanks !`
            &submittedResourceId=`70`
            &validate=`nospam:blank,
                username:required,
                password:required,        
                password_confirm:password_confirm=^password^,
                firstname:required,
                name:required`    
            &placeholderPrefix=`reg.`
            $postHooks=`setFullName`
        ]]
        


        "setFullname" is the name of the snippet/hook.

        PS- Would you be kind to have a look at my other post [Register] checkboxes and usergroups... I'm stuck with these checkboxes... . Thx in advance
          • 28215
          • 4,149 Posts
          Well, you can't set the 'fullname' field on the modUser object, as 'fullname' is a modUserProfile field, not a modUser field.
            shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
            • 1778
            • 659 Posts
            So I tried to modify my snippet to act on modUserProfile the new one is :
            <?php
            $username = $hook->getValue('username');
            if (empty($username)) {
                $hook->addError('user', 'Username field empty');
                return false;
            }
            
            $name = $hook->getValue('name');
            if (empty($name)) {
                $hook->addError('user', 'name field empty');
                return false;
            }
            
            $firstname = $hook->getValue('firstname');
            if (empty($firstname)) {
                $hook->addError('user', 'firstname field empty');
                return false;
            }
            
            $usrobject = $modx->getObject('modUser', array('username' => $username));
            $userid = $usrobject->get('id');
            if (empty($userid)) {
                $hook->addError('user', 'Error getting user object');
                return false;
            }
            $profile = $usrobject->getOne('Profile');
            if(empty($profile)) {
                $hook->addError('user', 'profile not found');
                return false;
            }
            
            $profile->set('fullname', $name . ' ' . $firstname);
            $profile->save();
            $usrobject->save();
            
            


            But it doesn't work, fullname still empty.... I think I will cry...
              • 28215
              • 4,149 Posts
              Have you tried:

              $profile = $hook->getValue('register.profile');
              $profile->set('fullname',$hook->getValue('name').' '.$hook->getValue('firstname'));
              $profile->save();
              return true;
              


              Make sure your postHook returns true. Also, add some debug calls in there to make sure you're even getting to the postHook update.

              Finally, remember that you can't send back error messages for postHooks - they occur after the user is registered, so there's no need to do validation. If you want validation, you'll need a validator or preHook.
                shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                • 1778
                • 659 Posts
                Time for dinner right now...
                I will try it after dinner...
                How do i "add some debug calls" in it ?

                Thanks a lot
                Cheers
                  • 28215
                  • 4,149 Posts
                  $modx->log(modX::LOG_LEVEL_ERROR,'My error message here');


                  Then check your error.log file for any messages.
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 1778
                    • 659 Posts
                    I tried your code and add some debug code but it doesn't solve the problem...
                    Nothing in the error log and the fullname is not set as expected (not set at all in fact). It's like the postHook was not fired at all...
                    Here is my code

                    $username = $hook->getValue('username');
                    if (empty($username)) {
                        $modx->log(modX::LOG_LEVEL_ERROR, 'error on username');
                        return false;
                    }
                    $name = $hook->getValue('name');
                    if (empty($name)) {
                           $modx->log(modX::LOG_LEVEL_ERROR, 'error on name here');
                        return false;
                    }
                    
                    $firstname = $hook->getValue('firstname');
                    if (empty($firstname)) {
                        $modx->log(modX::LOG_LEVEL_ERROR, ' error fname');
                        return false;
                    }
                    
                    $usrobject = $modx->getObject('modUser', array('username' => $username));
                    $userid = $usrobject->get('id');
                    if (empty($userid)) {
                        $modx->log(modX::LOG_LEVEL_ERROR, 'error userid');
                        return false;
                    }
                    
                    //$profile = $usrobject->getOne('Profile');
                    //if(empty($profile)) {
                    //    $hook->addError('user', 'profile not found');
                    //    return false;
                    //}$profile = $usrobject->getOne('Profile');
                    
                    $profile = $hook->getValue('register.profile');
                    if(empty($profile)){
                        $modx->log(modX::LOG_LEVEL_ERROR, 'error profile');
                        return false;
                    }
                    $profile->set('fullname', $hook->getValue('name') . ' ' . $hook->getValue('firstname'));
                    $profile->save();
                    return true;
                    
                    


                    I'm tired and a bit sick (and maybe I'm an idiot) but I can't have it to work... + the problem on checkboxes , Register drives me crazy...
                    • Hey,

                      That just might be a typo, but anso, did you see you are using $postHooks instead of &postHooks (probably spending too much time into PHP code ^^).