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

Unique custom user fields?

#1
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.
 

xf_phantom

Well-known member
#3
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
        }
    }