1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Unique custom user fields?

Discussion in 'XenForo Development Discussions' started by Booher, Dec 8, 2013.

  1. Booher

    Booher Member

    Is it possible to get the user's information in the PHP callback check for a custom userfield.

    Here is what I have:

    Code:
    <?php
    class MineVille_MCLogin
    {
        public static function validate($field, &$value, &$error)
        {
            $visitor = XenForo_Visitor::getInstance();
            $db = XenForo_Application::get('db');
            if(!preg_match("/^[a-zA-Z0-9_-]+$/", $value)) {
                $error = "This is an invalid MineCraft username.";
                return false;
            }
            if(file_get_contents("https://minecraft.net/haspaid.jsp?user=".$value) == "false") {
                $error = "This is an invalid MineCraft username.";
                return false;
            }
            return true;
        }
    }
    
    I need it to make sure nobody else is using the same value for that field, I was going to use the info for the currently logged in user, but that doesn't work if you're editing the user in the AdminCP.

    If anybody has any ideas, that would be fantastic.
     
    vfustar likes this.
  2. Booher

    Booher Member

    So does nobody know how to do this?
     
  3. xf_phantom

    xf_phantom Well-Known Member

    You could register a global variable inside your user datawriter ($_GLOBALS or XenForo_Application::set() ) to have access to the data (or even the datawriter) inside your callback method.

    My implementation is:

    PHP:
    //XenForo_DataWriter_User
      
    public function setCustomFields(array $fieldValues, array $fieldsShown null)
       {
       
    XenForo_Application::set('userfield_edit_userdwInstance'$this);
    parent::setCustomFields($fieldValues,$fieldsShown);
    }


    // callback:
           
    public static function test($field, &$value, &$error)
        {
            if (
    XenForo_Application::isRegistered('userfield_edit_userdwInstance')) {
                
    /** @var XenForo_DataWriter_User $userDataWriter */
                
    $userDataWriter XenForo_Application::get('userfield_edit_userdwInstance');
                
    // now you have access to the data
            
    }
        }
     
  4. Booher

    Booher Member

    I'll check and see if that works after I get off work.

    Thanks!
     

Share This Page