We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 73
    • 37 Posts
    I expected this to be very straight forward but after almost a day I am driven to posting in this forum.

    On the register form, I am trying to set the username to the email address value using &preHooks.

    I’ve tried a variety of ways, and have been feeding out the values to the page fine for checking, but the username field always ends up blank when the save has completed? It seems very easy to read values on the preHooks snippets, but to set them is not so straight forward and I cannot find documentation on how to do this anywhere!

    <?php
    
    /*
        Register prehook script
        Assigns email address to username
    */
    
    $email = $scriptProperties['fields']['email'];
    
    if(!isset($email) || $email == ''){
        $errorMsg = '<p>Please supply an email address</p>';
        $scriptProperties['hook']->errors['user'] = $errorMsg;
        return false; 
    
    }else{
        // None of these scripts below work!
        //$scriptProperties['fields']['username'] = $email;
        //$_POST['username'] = $email;
        $fields['username'] = $email;
        // when I uncomment this lin below I can see the values are populating the vars though!?!
        //$scriptProperties['hook']->errors['user'] = "fields array username = ".$fields['username'] . ";<br/>fields array email = ".$email;
        return true;
    }
    
    
    ?>
    


    Any advice, even just a simple line of code to show how to set the username field in a preHook, would be oh so appreciated.

    Thank you..
      • 73
      • 37 Posts
      Found this line on the formit documentation, thought they might be slightly similar, but unfortunately this is also not working:
      $scriptProperties['hook']->fields['username'] = $email;
      

      huh

      Quite odd how it is putting in an empty username field BTW, would have thought the validator should be catching that in any case? embarrassed
        • 28215
        • 4,149 Posts
        splittingred Reply #3, 16 years ago
        What are you trying to do here, just set the username for the new user as the email?

        If so, you can use the &usernameField=`email` property to do that.
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 73
          • 37 Posts
          Thank you so much for your reply Shaun.

          I was very excited to see this simple &usernameField property I had clearly overlooked, so I implemented it straight away.

          Unfortunately it is not working for me, and is having the same result as the previous attempts I was making; ie. no username being set at all!

          In my latest set up, in the register page I have:
          [[!Register? &usergroups=`Members`&usernameField=`email`&activation=`0`&submitVar=`login-register-btn`&successMsg=`<p>New user [[+fullname]] with email address [[+email]] has been added!</p>`&usernameField=`email`]]
          [[$lgnRegisterForm]]
          


          And the lgnRegisterForm template chunk is:

          <div class="register">
              <div class="registerMessage">[[+error.message]]</div>
              
              <form class="form" action="[[~[[*id]]]]" method="post">
                  <input type="hidden" name="nospam:blank" value="" />
                  
                  <label for="email">[[%register.email? &namespace=`login` &topic=`register`]]
                      <span class="error">[[+error.email]]</span>
                  <input type="text" name="email:email:required" id="email" value="[[+email]]" />
                  </label>
                  
                  <label for="password">[[%register.password]]
                      <span class="error">[[+error.password]]</span>
                  <input type="password" name="password:required:minLength=6" id="password" value="[[+password]]" />
                  </label>
                  
                  <label for="password_confirm">[[%register.password_confirm]]
                      <span class="error">[[+error.password_confirm]]</span>
                  <input type="password" name="password_confirm:password_confirm=`password`" id="password_confirm" value="[[+password_confirm]]" />
                  </label>
                  
                  <label for="fullname">[[%register.fullname]]
                      <span class="error">[[+error.fullname]]</span>
                  <input type="text" name="fullname:required" id="fullname" value="[[+fullname]]" />
                  </label>
                  
                  <label for="company">Company
                      <span class="error">[[+error.company]]</span>
                  <input type="text" name="company:required" id="company" value="[[+company]]" />
                  </label>
                  
                  
                  <br class="clear" />
          
                  [[+register.recaptcha_html]]
                  [[+error.recaptcha]]
                  
                  <div class="form-buttons">
                      <input type="submit" name="login-register-btn" value="Register" />
                  </div>
              </form>
          </div>
          


          Any ideas why this is still giving me grief? I really thought it would be so simple to set the username the same as the email address!

          Big thanks for any clues or hacks that can help me get this working undecided
          Toby
            • 73
            • 37 Posts
            Just updated to the latest login 1.2.0 rc2 and still no joy.

            My system is LAMP running:
            MODx version: Revolution 2.0.0-pl
            ExtJS Version: 3.2.1
            Smarty Version: 2.6.19
            PHPMailer Version: 2.0.4
            MagpieRSS Version: 0.7a
            Database type: mysql
            Database version: 4.1.22-standard
            Database charset: utf8

            Thanks..
              • 32525
              • 27 Posts
              Hey there,

              I was having the same problem trying to get a user to register when using the custom fields in the snippet. I think it’s because at no point were the fields converted back to ’username’, ’password’ and ’email’ which is expected by the register processor (which I think is using [tt]$profile->fromArray($fields);[/tt] but not enough time to go through that too!).

              So I have modified my Profile snippet, and added a few extra lines:

              if (empty($login->validator->errors)) {
              
                      /* do pre-register hooks */
                      $login->loadHooks('prehooks');
                      $login->prehooks->loadMultiple($preHooks,$fields);
              
                      /* process hooks */
                      if (!empty($login->prehooks->errors)) {
                          $modx->toPlaceholders($login->prehooks->errors,'error');
              
                          $errorMsg = $login->prehooks->getErrorMessage();
                          $modx->toPlaceholder('message',$errorMsg,'error');
                      } else {
              
                          /* Replace username, password and email fields that are modified */
                          if ($fields[$usernameField]!='username') {
                              $fields['username'] = $fields[$usernameField];
                              unset($fields[$usernameField]);
                          }
                          if ($fields[$passwordField]!='password') {
                             $fields['password'] = $fields[$passwordField];
                              unset($fields[$passwordField]);
                          }
                          if ($fields[$emailField]!='email') {
                              $fields['email'] = $fields[$emailField];
                              unset($fields[$emailField]);
                          }
              
                          $result = require_once $login->config['processorsPath'].'register.php';
                          if ($result !== true) {
                              $modx->toPlaceholder('message',$result,'error');
                          }
                      }
                  }


              This is now working for me as it should. Hope that helps both yourself and SplittingRed who seems to have a constant uphill climb with the amount of packages that he’s created!!! wink
                • 73
                • 37 Posts
                Ahh wicked thanks Wiseguy, this helped great grin

                I popped the relevant lines into the Register snippet, works a treat; although I did have to remove the unset commands as I needed to retain the same field for both username and email.

                So simple once you know, I’m still trying to get my head around this MVC/Classes way of doing things.

                In any case, you saved me from a quicky javascript bootstrap, thanks again..
                  • 4971
                  • 964 Posts
                  Hey guys, can you tell me where to put this code?

                  I was going to just use a hidden input for username with a value of email...
                  but then used what Shaun suggested &usernameField=email but as you already
                  know, it does not work.

                  I see that the snippet of code is for processing prehooks in Register...
                  but I am not using any prehooks in the register snippet!!

                  Am I completely lost? shocked
                    Website: www.mercologia.com
                    MODX Revo Tutorials:  www.modxperience.com

                    MODX Professional Partner
                    • 23510
                    • 168 Posts
                    Create a snippet:

                    $username = $hook->getValue(’username’);
                    $hook->setValue(’email’,$username);
                    return true;

                    add &preHooks=`yoursnippetname` to your Register snippet.
                      • 4971
                      • 964 Posts
                      I think this was fixed in Login 1.3
                        Website: www.mercologia.com
                        MODX Revo Tutorials:  www.modxperience.com

                        MODX Professional Partner