We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40088
    • 708 Posts
    Revo 2.6.3
    Login 1.9.5

    I have a ClassExtender field (screenName) that requires a minimum of three characters which correctly throws an error if less than three. The problem is the form still submits and the field is updated despite the error.
    [[!ExtUserUpdateProfile]]
    [[!UpdateProfile?
    	&preHooks=`FullName`
    	&excludeExtended=`email:required:email,login-updprof-btn,mediaFolder`
    	&validate=`nospam:blank,
    		username:required,
    		firstName:required,
    		lastName:required,
    		screenName:required:minLength=^3^,
    		email:required:email`
    	&useExtended=`0`
    ]]
    [[!SetUserPlaceholders]]

    <label for="screenName">Screen Name [[!+error.screenName]]</label>
    <input type="text" name="screenName" id="screenName" value="[[+screenName]]">

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

      Todd
      • 3749
      • 24,544 Posts
      The problem is that ExtUserUpdateProfile doesn't validate fields, and even if it did, it wouldn't receive the validation properties. All it does is update the custom fields and save the UserData object. It's finished before UpdateProfile runs and discovers and reports the error.

      It should be relatively easy to use JavaScript to validate the custom fields.

      You could also alter the ExtUserUpdateProfile snippet to do validation by copying the validation code from UpdateProfile. but I think the JS would be easier.
        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
        Hmmm... ok. Thanks.

        Is PHP also a validator option? The problem with js is that it's easily disabled.
          Todd
        • discuss.answer
          • 3749
          • 24,544 Posts
          Wait, I thought of a much easier way:

          Add the three extra lines to the ExtUserUpdateProfile snippet, just below if ($submission) {.
          if ($submission) {
             if (strlen($_POST['screenName']) < 3) {
                return "";
             }
          
          ... 
          }


          That way, the custom field won't be updated and UpdateProfile will display the error message for you.
            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
            Very nice, it works great. Thank you.

            Just so I understand, if/when ClassExtendeder is updated this will be overwritten, correct?
              Todd
              • 3749
              • 24,544 Posts
              Yes it will, though you can rename the snippet and call that new snippet in the tag. That way it won't get overwritten.
                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
                • 38783
                • 571 Posts
                I'm glad I came across this post. It points to a solution to a problem I had only just noticed where the data in 'required' ClassExtender fields that had been successfully populated during registration was occasionally being removed when people were updating their profiles.

                To prevent the existing data in four ClassExtender fields from being overwritten I have added the following code. (field names are : firstName, lastName, businessAddressCounty, businessAddressPostcode)

                It seems to work, but I wonder if there is a better way of writing the code, rather than repeating the whole thing four times?

                if ($submission) {
                if (strlen($_POST['firstName']) < 1) {
                      return "";
                   }
                if (strlen($_POST['lastName']) < 1) {
                      return "";
                   }
                if (strlen($_POST['businessAddressCounty']) < 1) {
                      return "";
                   }
                if (strlen($_POST['businessAddressPostcode']) < 1) {
                      return "";
                   }
                    $modx->request->sanitizeRequest();
                    $dirty = false;
                    foreach ($fields as $key => $value) {
                        if (isset($_POST[$key])) {
                            if ($value !== $_POST[$key]) {
                                $data->set($key, $_POST[$key]);
                                $dirty = true;
                            }
                        }
                    }
                
                    if ($dirty) {
                        $data->save();
                    }
                }
                
                return '';
                


                Or should I be using something different like
                if (empty($_POST['firstName'])) {
                      return "";
                   }
                  If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                  email: [email protected] | website: https://andytough.com
                  • 3749
                  • 24,544 Posts
                  If you don't need to check the length, this would be correct:

                  if ( (! isset($_POST['firstName'])) || empty($_POST['firstName'])) {
                        return "";
                  }
                    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
                    • 38783
                    • 571 Posts
                    Thank you Bob.
                      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                      email: [email protected] | website: https://andytough.com
                      • 3749
                      • 24,544 Posts
                      On second thought, I'm not sure the isset() is necessary. You might check the $_POST array with them empty to see if they're always set. if they are, you don't need the isset().

                      Note that it's normal to see an empty array when the form is first loaded. It's what happens on submit that counts.

                      echo "POST: " . print_r($_POST, true);
                        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