We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!

Answered Update Username

    • 40088
    • 708 Posts
    Revo 2.6.3

    I included the username field in the UpdateProfile form but changes are not being saved. No errors, the value just stays the same. Works fine when editing in the Manager Menu > Users > User.
    [[!UpdateProfile?
    	&preHooks=`FullName`
    	&excludeExtended=`email:required:email,login-updprof-btn,mediaFolder`
    	&validate=`username:required:minLength=`^6^`,
    		email:required:email`
    	&useExtended=`0`
    ]]

    <form action="[[~[[*id]]]]" method="post">
        [[+login.update_success:is=`1`:then=`[[%login.profile_updated? &namespace=`login` &topic=`updateprofile`]]`]]
    
        <label for="username">[[!%login.username]] [[!+error.username]]</label>
        <input type="text" name="username" id="username" value="[[+username]]">
        …
    </form>
    

    This question has been answered by BobRay. See the first response.

      Todd
      • 3749
      • 24,544 Posts
      I suspect that UpdateProfile does just what its name suggests -- update the user's modUserProfile object. The username and password are part of the modUser object.

      You might be able to write a postHook that would update the username.
        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
        • 40088
        • 708 Posts
        Ah, I see. I didn't realize they were separate things.

        Is there another way to allow users to update their username from the front-end?
          Todd
        • discuss.answer
          • 3749
          • 24,544 Posts
          I don't know of one. I think it would take a custom snippet, though it might be possible to use a custom postHook with UpdateProfile.

          The hook would look something like this (untested):

          $submittedName = $hook->getValue('username');
          
          if ( (!empty($submittedName) ) && ($submittedName !== $modx->user->get('username') ) ) {
             $modx->user->set('username', trim($submittedName) );
             $modx->user->save();
          }





            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
            • 40088
            • 708 Posts
            Thanks Bob, I'll try your example and let you know.

            Going back to your first reply about modUser and modUserProfile, is there a specific reason why two different objects would be used for the user profile? Could it be a security thing?

            Not knowing how or why MODX was designed the way it is it seems, with my limited understanding, that the user profile info would all fall under the same umbrella (object), so to speak.
              Todd
              • 40088
              • 708 Posts
              Once again Bob you've knocked it out of the park. Your snippet works a treat.

              Thank you.
                Todd
                • 46886
                • 1,154 Posts
                This seems strange, one workaround would be to let them modify the users fullname, which can definitely be changed, in fact its in the sample updateprofile snippet in the docs. And indeed username is not one of the values provided.

                I don't know what your site looks like but when you build pages you can let users be identified by fullname rather than username and then the lack of changing would be less serious.

                Eko, I think the honest answer is that Modx doesn't really deal with user issues, its one of the gaps in the system. Even the extended profile in a JSON is probably not optimal. Keep in mind a lot of sites don't need users at all.

                However there are tools like BobRay's classExtender that solve these problems pretty easily. Remember Modx is supposed to be a contained system, where ALL customization should happen outside that core, never hack the core because you can always modify an extra, snippet or other tool. So its supposed to be compact.

                If it handled these issues, it would be great, but what would happen when you didn't like those default behaviors?

                So, users are an extra module by necessary. Doesn't make it easier to climb that hill, though. But, most of the tools we need are out there. [ed. note: nuan88 last edited this post 5 years, 11 months ago.]
                  • 40088
                  • 708 Posts
                  Hi nuan88,

                  Users are identified by their first name, not their full name or username.

                  Thanks for your explanation and yes, I see your point. There often seems to be at least one solution to any problem.
                    Todd
                    • 3749
                    • 24,544 Posts
                    I'm glad (and a little surprised) that the snippet worked out of the box. wink

                    I don't really know the thinking behind the separation of the modUser and modUserProfile object into separate objects in two different tables. It may be speed-related, since the modUser object is quite small. Also, given a user ID, you can pull either the modUser object or the modUserProfile independently, depending on what you need. If you have a user ID you can get the user's email, for example, without retrieving the modUser object since the modUserProfile contains the user ID in its internalKey field.

                    Another possibility is that in the very early Etomite days, the modUser object was all there was. When they realized that people needed more user information in the DB, they created a separate table for the extra data rather than modifying the modUser object, and included the extended field so that you could put whatever you needed into the user profile.

                    For info on what's where:

                    modUser: https://bobsguides.com/modx-object-quick-reference.html#modUser

                    modUserProfile: https://bobsguides.com/modx-object-quick-reference.html#modUserProfile
                      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
                      • 46886
                      • 1,154 Posts
                      Wow classic. Yeah in the early days user capability would have been much more bespoke software, custom modules. And forums I guess did most of the work too. Modx early was definitely not a forum, we can say that. As we have seen things like commenting systems have waxed and waned over the years, remember Gravatar? More like Gravetar lol! Core websites don't always need a lot of user functions, and those can be pawned off on subsidiary systems usually.

                      But this sentence makes me wonder

                      If you have a user ID you can get the user's email, for example, without retrieving the modUser object since the modUserProfile contains the user ID in its internalKey field.

                      is this a two way street, or is the one easier or better to get than the other? You chose one way over the other, is it preferable for some reason? Or you are just emphasizing the independence of the two objects? [ed. note: nuan88 last edited this post 5 years, 11 months ago.]