XF 2.1 [SOLVED] Addon option callback function signature

asprin

Active member
So I'm using a validation callback to validate the value entered by the user in my addon's option page.
1581157485056.png

The Options.php class is as follows:
PHP:
<?php
namespace FC\Callbacks;

class Options {

    public static function validateData($field, &$value, &$error)
    {      
        $sm = \XF::db()->getSchemaManager();      
        if($sm->columnExists('xf_user', $value))
        {
            return true;
        }
       
        $error = 'Column was not found.';
        return false;      
    }

}
Now when I try to save a value on the options page, I'm getting the following error:
Parameter 2 to FC\Callbacks\Options::validateData() expected to be a reference, value given in src\XF\Entity\Option.php at line 206

I know that it's referring to the second argument of the callback function, but doesn't the & denote that it is being passed as reference? If not, what is the expected signature of the function?
 
.hello. Your function should look like this:
PHP:
public static function verifyOption(&$value, \XF\Entity\Option $option, $optionId)
{
    //
}

You can skip last two arguments if you don't need it.
 
Yep. That does the trick. Just one last bit - How would one show a message to the user in the frontend in case I want to return false and let them know it's not a valid value?
 
Yep. That does the trick. Just one last bit - How would one show a message to the user in the frontend in case I want to return false and let them know it's not a valid value?
.you can add an error to Option entity
PHP:
public static function verifyOption(&$value, \XF\Entity\Option $option)
{
    if ($value === 'lol')
    {
        $option->error(\XF::phrase('addon_this_option_value_cannot_be_lol'));
        return false;
    }
    
    return true;
}
 
Top Bottom