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

    I try to use getObjet to check if email address is already recorded during registering (Login Register).

    When I try this to check if usernam exist, it works :
    if ($user = $modx->getObject('modUser', array('username'=> 'user1'))){
     
       //do something with existing user
      return "already exists !";  
    }
    


    But this piece of code returns nothing although email exists in user :
    if ($user = $modx->getObject('modUser', array('email'=> '[email protected]'))){
     
       //do something with existing user
      return "already exists !";  
    }
    
    


    any idea ?
      • 17301
      • 932 Posts
      The email is in the profile object

      $profile = $user->getOne('Profile');
      $email = $profile->get('email');
      
        ■ email: [email protected] | ■ website: https://alienbuild.uk

        The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
        • 53073
        • 10 Posts
        Quote from: lkfranklin at Mar 23, 2017, 07:18 PM
        The email is in the profile object

        $profile = $user->getOne('Profile');
        $email = $profile->get('email');
        

        The problem is that I don't know the user ID, then I cannot see how to retrieve a user profile.

        I found a solution but I guess the is a more 'elegant way'

        here is the custom validator designed to work with the Register (Login) snippet :

        <?php
        /***********************************************
         * Snippet: xvRegCheckEmail
         *
         * Check if email exists
         * usage :
         * &validate=`email:xvRegCheck=[[+email]]`
         * &customValidators=`xvRegCheckEmail`
         */
        $value_tocheck = $scriptProperties['value'];
        $value_tocheck = $modx->quote($value_tocheck);
        //return $value_tocheck;//la valeur du champ
        $sql = "SELECT * FROM modx_user_attributes WHERE email = $value_tocheck";//ok
            foreach ($modx->query($sql) as $row) {
            // do something if email exists
            $result .= $row['id'] .'<br/>';
        }
        if($result){
            //$output = "There is already a user with this email !" .$field_tocheck. " = " . $value_tocheck . " !";
            //$output = "1";
            return 'There is already a user with this email ! <a href="[[~471]]?e='.$value_tocheck.'" target="_blank"><i class="fa fa-question-circle" aria-hidden="true"></i></a>';
        } 
        return true;
        
          • 3749
          • 24,544 Posts
          The slightly more elegant way would be this: wink

          $result = $modx->getObject('modUserProfile', array('email' => $value_tocheck));
          
          if ($result) {
             // etc.
          }
          
            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