We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38755
    • 43 Posts
    EDIT: seek and ye shall find... I had searched but somehow missed this result which looks like it will do the trick: http://modxcms.com/forums/index.php?topic=33327.0

    My only question is if there is a good way to do this as a plugin, I prefer not to edit core files in order to avoid having to track the changes and redo them after updates.


    Original Post:
    As the subject says, I’m wondering what the best approach to making the email be the username is. I could make the username field hidden and use javascript to assign the email to it... but that doesn’t seem particularly reliable.

    I’ve just started using this, perhaps the best way is to use one of the API events to duplicate the email to the username? Another consideration is keeping it up to date (if someone updates their email address the change should apply to the username as well).. I suppose using another API call on profile updates would address this?

    If anyone has a suggestions or example snippets to bootstrap my learning curve that would be great!  smiley

    Thanks,
    Gabe
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      You can use Javascript to accomplish this, too... it’s not as robust, but consider the following:

      <form id="wlpeRegisterForm" name="wlpeRegisterForm" action="[~[*id*]~]" method="POST" onsubmit="wlpeRegisterForm.email.value=wlpeRegisterForm.username.value;">
      	<input type="text" id="wlpeRegisterEmail" name="email" />
      	<input type="text" id="wlpeRegisterUserName" name="username"/>
      </form> 


      You can hide the email field once you’ve verified that it works... and then you’d want to label your username field as an "email" field.
        • 38755
        • 43 Posts
        Yeah, I considered that and had it working exactly the way you suggest but I don’t want to rely on the user having javascript enabled. Allowing them to login using either a username or their email address suits my situation well tho smiley
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          Yeah, I thought about the possibility of javascript being disabled... but I think in this case the solution would still work. If Javascript were off, then the hidden email field would never get populated, and it’s required (at least for verification registrations), so it would trigger an error. You could change your messaging to tell your users to enable Javascript... but yes, it would be more robust to handle this with PHP on the backend.
            • 27442
            • 103 Posts
            Maybe this is too simplistic (I haven’t tested it), but why can’t you just hide one field or the other and populate both with the same post info?


            <label for="wlpeUserRegisterEmail">Email Address
                <input id="wlpeUserRegisterEmail" type="text" name="email" value="[+post.email+]" />
            </label>
            <label for="wlpeUserRegisterUserName">
                <input id="wlpeUserRegisterUserName" type="text" name="username" value="[+post.email+]" type="hidden" />
            </label>
            <label for="wlpeUserRegisterPassword">Password
                <input id="wlpeUserRegisterPassword" type="password" name="password" value="[+post.password+]" />
            </label>
            <label for="wlpeUserRegisterPasswordConfirm">Confirm Password
                <input id="wlpeUserRegisterPasswordConfirm" type="password" name="password.confirm" value="[+post.password.confirm+]" />
            </label>
             
              • 38755
              • 43 Posts
              The problem here (as I understand it) is that the post info isn’t available on a fresh page load (as the registration page would typically be seen). It’s only available after the page has posted to itself, which makes it perfect for keeping the form fields populated after an attempted post has a validation error or similar, but doesn’t work for capturing newly entered data that hasn’t passed in from a previous post to the page.
                • 9207 ☆ A M B ☆
                • 2,475 Posts
                Are you still trying to solve the username/email problem? Or are you looking at some other issues as well?

                The $_POST data will be available to whatever page the form posts to... be it the registration success page or the same page containing the form.

                I’m not sure what you meant here:
                doesn’t work for capturing newly entered data that hasn’t passed in from a previous post to the page.

                Maybe I’m not following what you’re trying to do. Can you clarify?
                  • 38755
                  • 43 Posts
                  Hi Everett, thanks for the response but I should clarify that this issue is solved for my situation. As I indicated a couple posts back allowing the user to log in via either username or email actually works better for our needs. The only part of that solution that isn’t ideal is applying it as a patch against the core files.

                  In regards to my reply to sjh_vt, and the quoted portion you asked about I was pointing out that populating the fields using posted data wouldn’t help in the case of a freshly loaded (not posted to) form. The registration form being one that generally would be loaded fresh, without posted data passed in. While an extra step could be added to take the posted email and assign it to the username before reposting everything for processing or something similar, just making the email available as a means to login worked out great for our purposes.

                  To summarize: the issue in the original post is addressed, and things are working fine, no issues (by allowing the user to log in with either their username or email address). As far as I’m concerned any further discussion here is just for the sake of discussion, bouncing alternative ideas around, or answering another poster’s question should one be asked. wink