XF 2.0 Custom User Field callbacks

Drakeus

Member
I am trying to create a callback to validate the input of a custom user field against a database. I have looked through the few discussions on here for the php callbacks for XF2 but unfortunately have not been able to make it work based on the information I have found. in the admin cp for the call back i am entering:

Code:
GW2\Callback\UserField\gw2::validate

my file path is /addons/GW2/Callback

and UserField.php is as follows:

PHP:
<?php

class GW2\Callback\UserField\gw2{

    public static function validate ($field, &$value, &$error){

$servername = "xxxxxxxx";
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$dbname = "xxxxxxxx";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT member FROM gw2_roster WHERE member = '$value'";
$member = $conn->query($sql);

        //this Should search the db for $value
        if ($member->num_rows > 0)

        {
        //if rows returned greater than 0 continue with registration          
            return true;
        }
        else
        {  
            $error = 'Guild Wars 2 account does not exist in guild please try again later. <br>If you just joined the guild please allow up to 12 hours for API to be queried.';
            return false;
        }
    }
}

I had this working for XF1.5 and am wanting to upgrade my forums to XF2.0 but will not be doing so until i get this to work.

any help would be greatly appreciated.
 
Last edited:
long story short:

You need to add namespace and that will be your addons root folder followed by the subfolder your file is in and in the call back field you need to enter addon_name\sub_folder\file_name
in your file the namespace is going to be the path from above minus your file name and your class name will be your file name.

so after a bit more experimenting and playing around I figured it out and figured I put it here in case someone needs that little extra push to grasp it like I did, so here goes my attempt at an explanation:

for example if your path to the call back is:

/src/addons/GW2/Callback

in the PHP call back field you need to enter:

GW2\Callback\UserField::validate

and in your file you need to put:

PHP:
//do not include src or the addons folder in the name space
namespace GW2\Callback;

//class is your file name
class UserField{

//your method name goes here
public static function validate ($field, &$value, &$error){
 
PHP:
    public static function validate ($field, &$value, &$error){   
    
        $sql = "SELECT member FROM gw2_roster WHERE member = '$value'";
        var_dump($sql);
        
        }
42900390785_a20e4092b9_o.png

string(60) "SELECT member FROM gw2_roster WHERE member = 'test' AND 1=1'"

Sounds good. For your own safety, you should read this: https://xenforo.com/xf2-docs/dev/entities-finders-repositories/
Or at least implement prepared statements. Or at the very least implement some basic escapes.
 
@S Thomas , thank you for the feed back and advice, and maybe you can answer this question for me as well. With the above code for my call back I'm trying to take the value of another custom field (gw2_rank) and insert that into my query to check to see if $value and {$xf.visitor.custom_fields.gw2_rank} return a result so i can set up an auto rank assignment.

Also right now the entire forums is set up in a test environment on a VM with no internet access till i get all of this working and figure out the security aspects. The data for the DB that i am doing a query against has been manually populated by with with my test values

the issue I'm facing is I don't exactly know how to use {$xf.visitor.custom_fields.gw2_rank} in my query to get that value.

ex

Code:
gw2_roster = test.5968
gw2_rank = Officer

$rank = {$xf.visitor.custom_fields.gw2_rank};

SELECT member FROM gw2_roster WHERE member = '$value' AND rank = '$rank';

so in essence how do i make $rank actually equal the value of what they input in the gw2_rank field?

so far the XF manual has only given me this {$xf.visitor.custom_fields.gw2_rank}
 
Last edited:
Guessing:
$rank = \XF::visitor()->custom_fields->gw2_rank
Haven't tried it.

Edit:
Your code above, does that work in a template?
It should be
{$xf.visitor.Profile.custom_fields.gw2_rank}
so the PHP part would be
$rank = \XF::visitor()->Profile->custom_fields->gw2_rank
 
Back
Top Bottom