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

    I have a site which requires the username to be numerical (due to the user being registered on PAPER previously).

    I foudn thanks to the Forum that I cannot register purely numerical usernames in Modx.

    So, I am pre-pending all users with 'u' - so if they enter '123456' their username becomes 'u123456'.

    This all works very well - apart from if a user registers invalid information.

    So if they submit the registration form, but forget to enter their email address, the form will show errors, and the username they entered will display as u123456.

    So, when they correct the form, and hit submit, it will prepend their username with 'u' again - therefore their username will become 'uu123456'.

    Any ideas how to stop this from happening?

    Thanks
    Andy
      • 3749
      • 24,544 Posts
      If you're adding the prefix with a snippet, either don't update the input field from the $_POST on submission, or check for an existing u before adding the prefix.

      Feel free to post the existing code.
        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
        • 51020
        • 670 Posts
        Quote from: BobRay at May 27, 2016, 01:09 PM
        If you're adding the prefix with a snippet, either don't update the input field from the $_POST on submission, or check for an existing u before adding the prefix.

        Feel free to post the existing code.

        Thanks for this.

        This snippet to prepend the 'u' for registration is this:
        <?php
        if (isset($_POST['registerbtn'])) {
        $_POST['username:required'] = "u".$_POST['username:required'];
        }


        The snippet to prepend the 'u' for login is this:

        <?php
        if (isset($_POST['Login'])) {
        $_POST['username'] = "u".$_POST['username'];
        }

        The tpl that formats the email with their login details is this:

        username: <strong>[[+username:stripString=`u`]]</strong>


        So that it removes the leading 'u' to avoid confusion.

        The above all works - until someone submits a form without filling out all the fields, and gets an error.
        But I'm not sure how to not update the input field from the $_POST on submission? Or as you suggested, to check for an existing u before adding the prefix?

        I have no idea how to do this?

        Thanks
        Andy


          • 3749
          • 24,544 Posts

          For register:

          Replace this:

          $_POST['username:required'] = "u".$_POST['username:required'];


          With this:

          if ($_POST['username:required'][0] !== 'u') {
              $_POST['username:required'] = "u" . $_POST['username:required'];
          }


          And for Login:

          replace:

          $_POST['username'] = "u".$_POST['username'];


          With this:

          if ($_POST['username'][0] !== 'u') {
              $_POST['username'] = "u" . $_POST['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
            • 51020
            • 670 Posts
            Quote from: BobRay at May 28, 2016, 04:55 PM

            For register:

            Replace this:

            $_POST['username:required'] = "u".$_POST['username:required'];


            With this:

            if ($_POST['username:required'][0] !== 'u') {
                $_POST['username:required'] = "u" . $_POST['username:required'];
            }


            And for Login:

            replace:

            $_POST['username'] = "u".$_POST['username'];


            With this:

            if ($_POST['username'][0] !== 'u') {
                $_POST['username'] = "u" . $_POST['username'];
            }




            This is great Bob thanks - the only problem is that when submitting an incomplete register form, the form shows the prepended 'u' on in the username field - is there anyway to not print this, so that users don't wonder where the 'u' came from - ideally it would never be visible to users.

            Is that possible, or can you not control what it shows in the input fields after a failed submission?

            Thanks
            Andy
              • 3749
              • 24,544 Posts
              It must have done that before, since your code already prepended the 'u'. Since the $_POST array is used to get the data, I'm not sure you can change that behavior. You might be able to set the placeholder with the un-prepended value, but I suspect that it will be reset later before the form is rendered.

              A better approach might be a plugin attached to OnUserActivate that adds the 'u' if it's not already there.

              <?
              $uName = $user->get('username');
              if ($uName[0] !== 'u') {
                  $uName = "u" . $$uName;
                  $user->set('username', $uName);
                  $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
                • 51020
                • 670 Posts
                Quote from: BobRay at May 31, 2016, 01:05 PM
                It must have done that before, since your code already prepended the 'u'. Since the $_POST array is used to get the data, I'm not sure you can change that behavior. You might be able to set the placeholder with the un-prepended value, but I suspect that it will be reset later before the form is rendered.

                A better approach might be a plugin attached to OnUserActivate that adds the 'u' if it's not already there.

                <!--?
                $uName = $user--->get('username');
                if ($uName[0] !== 'u') {
                    $uName = "u" . $$uName;
                    $user->set('username', $uName);
                    $user->save();
                }

                Hi Bob - yes it has always done that - I just wondered if there was a way to prevent it.
                So the plugin you outlined above would replace the prepending snippet I added - is that correct?

                Thanks
                Andy
                  • 3749
                  • 24,544 Posts
                  Yes, that's right.
                    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