We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43084
    • 34 Posts
    FormIt 2.2.0
    Login 1.8.1
    MODX Rev. 2.2.7

    Hello,

    Since Login doesn't have an option to ask for the email address twice for verification when updating the user's profile, and since UpdateProfile does not accept Custom Validators according to the documentation, I thought I could use FormIt to create a 'Change Email' page (similar to the 'Change Password' page in Login) which would get the email address, ask for it again, validate the fields and then use UpdateProfile as a hook to process the form. This I thought after checking some similar and creative approaches in this same forum.

    The idea behind this approach was to take advantage of what I already had installed instead of creating my own snippet. Furthermore, if this worked, I could use the same principle and use FormIt and Login together to solve other issues. But I guess I'm not doing it right.

    The thing is, when I add the line with the UpdateProfile hook, FormIt stops validating anything in the form. That is, I can enter a blank value or an invalid email and the form gets processed without errors and the appropriate email field gets updated. UpdateProfile works but FormIt stops validating.

    After adding some fields it was apparent that UpdateProfile was being executed when the page was initially loaded and before the form was even submitted, even when UpdateProfile was called as a Hook. This was evident when placeholders like [[!+email]] and [[!%login.email]] showed the preloaded values when the page was initially loaded.

    Is it possible to make these two packages work together like this or should I just forget this idea?


    This is the code for my Change Email page:

    [[!FormIt?
    &customValidators=`ConfirmEmail`
    &validate=`
        nospam:blank,
        email:required:email,
        confirm_email:ConfirmEmail=^email^`
    
    &hooks=`[[!UpdateProfile? &useExtended=`0`]]`
    ]]
    
    <p>Current email: [[!+email]]</p>
    <div class="update-profile">
        <div class="updprof-error">[[+error.message]]</div>
        [[+login.update_success:if=`[[+login.update_success]]`:is=`1`:then=`[[%login.profile_updated? &namespace=`login` &topic=`updateprofile`]]`]]
      
        <form class="form" action="[[~[[*id]]]]" method="post">
            <input type="hidden" name="nospam" value="" />
      
            <label for="email">New [[!%login.email]]
            </label>
            <input type="text" name="email" id="email" value="[[!+fi.email]]" />
            <span class="error">[[!+fi.error.email]]</span><br />
    
            <label for="confirm_email">Confirm New Email:
            </label>
            <input type="text" name="confirm_email" id="confirm_email" value="[[!+fi.confirm_email]]" />
            <span class="error">[[!+fi.error.confirm_email]]</span><br />
    
            <br class="clear" />
      
            <div class="form-buttons">
                <input type="submit" name="login-updprof-btn" value="[[!%login.update_profile]]" />
            </div>
        </form>
    </div>


    ConfirmEmail is the custom validator that checks if confirm_email and email are the same. This validator works fine (as well as all the others) when the hook line is removed.

    Thanks in advance for yout help!

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

    • This might be a bit hacky but it might actually work IDK it's just a theory, why don't you just use FormIt to validate the email and then have it run the UpdateProfile code on a success message that way it will only update the profile after it has passed the validation.

      You might need to use FormIt store to save the email value and then FormItRetriever in the success message to set the email value and then run the UpdateProfile code.

      I'm not sure if this will actually work its just something I thought of reading your problem, let me know if it works.

      Good Luck
        Benjamin Marte
        Interactive Media Developer
        Follow Me on Twitter | Visit my site | Learn MODX
      • I'm also trying to help you, but I don't know why I just can't make the UpdateProfile snippet to work, it doesn't throw any errors. The other snippets just work fine. Have a sleep, tomorrow I'll try again.
          • 43084
          • 34 Posts
          Quote from: benmarte at Apr 11, 2013, 03:36 PM
          This might be a bit hacky but it might actually work IDK it's just a theory, why don't you just use FormIt to validate the email and then have it run the UpdateProfile code on a success message that way it will only update the profile after it has passed the validation.

          You might need to use FormIt store to save the email value and then FormItRetriever in the success message to set the email value and then run the UpdateProfile code.

          Thanks, Benmarte for the suggestion. This sounds good, even if, as you say, might look a bit hacky. I'll try it right away and let you know how it went. Thanks again!
            • 43084
            • 34 Posts
            Quote from: nepheus at Apr 11, 2013, 05:09 PM
            I'm also trying to help you, but I don't know why I just can't make the UpdateProfile snippet to work, it doesn't throw any errors. The other snippets just work fine. Have a sleep, tomorrow I'll try again.

            Thanks anyway Nepheus!

            That's wierd. In my case, UpdateProfile is the only thing working fine. It's a pity since I would really love to have something like FormIt to process a form before Login does in order to solve other issues.

            In the meantime, I'll try Benmarte's suggestion, but please let us know if you find another way. And thanks again!
            • discuss.answer
              Hi,

              Last night my brain was so jammed that I coundn't realize I forgot to log-in on the frontend using Login snippet. Today I've just felt that it was so idiotic problem by me.

              I've done a custom snippet to work with UpdateProfile snippet, using its preHooks option. You can have a look as below, this is tested under my setup and it works just fine:

              recheckEmail snippet
              <?php
              $modx->log(MODX::LOG_LEVEL_ERROR, 'processing hook for confirm email'); //begin debug
              $modx->log(MODX::LOG_LEVEL_ERROR,  $hook->getValue('email')); //test get value
              
              $email = $hook->getValue('email');
              $confirm_email = $hook->getValue('confirm_email');
              $user = $login->getUserByField('username', $modx->getLoginUserName());
              
              $modx->log(MODX::LOG_LEVEL_ERROR, $user->get('email')); //test getting user email
              
              //check if email is changed?
              if (strcmp($email, $user->get('email'))==0)
                  return true; //no change, so just return the form without any errors
              
              //else compare and validate the confirm_email field
              
              if (strcmp($email,$confirm_email)==0) //validated equal fields
                  return true;
              else{
                  $hook->addError('confirm_email','Please confirm your email');
                  return false;
              }
              


              In your update email resource, include the UpdateProfile snippet:
              [[!UpdateProfile? &validate=`fullname:required,email:required:email`&preHooks=`recheckEmail`&useExtended=`0`]]


              And in the chunk, put some lines to confirm email:

               <label for="confirm_email">[[!%login.confirm_email]]
                     <span class="error">[[+error.confirm_email]]</span>
               </label>
               <input type="text" name="confirm_email" id="email" value="[[+confirm_email]]" />
              


              Comment out the $modx->log() when you want to use it in production, it's only for debugging in case you aint aware of.

              Good luck.
                • 43084
                • 34 Posts
                Quote from: nepheus at Apr 12, 2013, 07:57 AM
                I've done a custom snippet to work with UpdateProfile snippet, using its preHooks option. You can have a look as below, this is tested under my setup and it works just fine:

                Wow! Thank you so much for the trouble!
                Right now I'm dealing with some server issues after I tried to upgrade my MODX version, but as soon as I have it running again I'll be sure to try this too.

                It's all in the name of learning, after all. =)

                Thanks a lot for the help!
                • Quote from: pcamelo at Apr 12, 2013, 09:03 AM
                  Quote from: nepheus at Apr 12, 2013, 07:57 AM
                  I've done a custom snippet to work with UpdateProfile snippet, using its preHooks option. You can have a look as below, this is tested under my setup and it works just fine:

                  Wow! Thank you so much for the trouble!
                  Right now I'm dealing with some server issues after I tried to upgrade my MODX version, but as soon as I have it running again I'll be sure to try this too.

                  It's all in the name of learning, after all. =)

                  Thanks a lot for the help!


                  Hope you will get luck with the upgrade.

                  About the code, the preHooks is not documented in UpdateProfile snippet so I guess that's why you're hestiate writing the code.

                  But when you look into the code, I found it's the best documentation though. Don't scare of reading the code, I think it's the point afterall. By the way, if you find your problem is solved, please mark it as solved.
                    • 43084
                    • 34 Posts
                    Sorry I delayed so much in answering. After the issues with the upgrade were fixed I was left pretty knocked out for a while.

                    First, Benmarte, I tried your suggestion and managed to pass the parameters to a success page that called the UpdateProfile snippet, but the value was not updated just by calling it, and I couldn't figure out how to process the email value without adding another form to that success page (which would have looked awkward to say the least). Anyway, thanks for suggesting it since I learned quite a bit form it.

                    And, finally, Nepheus, I applied your custom code and it worked great! Now I don't have to use FormIt at all. I would have liked to know how to fuse those two components together, but by doing this excercise I think I came up with an alternative to my other problem without needing to use FormIt. Thank you very much for your help and the time invested in the code above.

                    Cheers!