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

Custom user field callback, validate value 1.x

For when you need special code to validate the value entered.

  1. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Jake Bunce submitted a new resource:

    Custom user field callback, validate value (version 1.x) - For when you need special code to validate the value entered.

    Read more about this resource...
    Brad L likes this.
  2. Infuscu

    Infuscu Member

    Wonderful, will play around with it and see if I can figure out how to make it validate against the username and not a custom field. I need to validate against a database of usernames ..

    But anyway this was exactly what I was looking for .. thank you :)
  3. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    This resource contains code you can use to run queries and even connect to a second database if needed:


    That should be useful for what you want to do. You can use the same code inside of the function for this callback to get a database and run queries on it.

    Let me know if you have any questions.
  4. Infuscu

    Infuscu Member

    Yea, that is exactly what I need - thank you for the resource, makes it even easier!

    My main problem, however, is that I would like to avoid a custom_field and validate against the Name under registration instead. The reason is, that I would like my users to hold the same name in XenForo and the other application. Basicly what I'm searching for is a way to add a PHP Callback to the Name aka here (not that I need an interface for it):


    If you have any hints how to achieve that I'd be very grateful.

    If this proves too difficult for me to solve, I will probably stick with a custom user field and for that your resources are absolutely perfect!
  5. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    That is different. You would have to extend the user datawriter:

  6. Robert F Schmitz

    Robert F Schmitz Well-Known Member

    Jake thank you for pointing me this way. What code do I need to utilize in the Userfield.php if I want to check a member name that someone enters against the registered members? I learn this stuff as I do and I haven't done yet. :p

    In the end, I want to have something similar to the 'referred by' field that vb has.
  7. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    I would refer to my previous post:

    That would allow you to query the xf_user table in the database to see if the input value matches an existing username.
    Rob Fritz likes this.
  8. Mythotical

    Mythotical Well-Known Member

    In your example you use $value, does that need to change to the name of our custom field?

    Also when using custom functions such as matching from my own table in the db, would I do something like this:
    if ($value == $mytable['mycolumn'])
    Same question above except when used in DataWriter_User:
    if ($this->get('xfpoints_promotion_code') == $promo['code'])
    if($this->get('field''xfpoints_promotion_code') == $promo['code'])
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member


    $value is a variable containing the value entered into that field by the user. You can operate on that variable to check and validate the user-submitted value using whatever means you want.
  10. Ludachris

    Ludachris Well-Known Member

    Hey Jake would you have a few other example uses of the PHP callback? I'm trying to see if this is something I can make use of... just can't wrap my head around it.
  11. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    What are you trying to do with it?
  12. Ludachris

    Ludachris Well-Known Member

    That's the thing, I don't know what it "could" be used for... just trying to find out some example uses for it, see what others are using it for. Maybe that will give me some ideas on how I could make use of it.
  13. Tom Christian

    Tom Christian Active Member

    @Jake Bunce I'm getting the following error when the callback returns false:

    Server Error
    Parameter 2 to Callback_UserField::validate() expected to be a reference, value given
    [*]call_user_func_array() in XenForo/DataWriter/Option.php at line 346
    [*]XenForo_DataWriter_Option->_validateOptionValuePreSave() in XenForo/DataWriter/Option.php at line 211
    [*]XenForo_DataWriter_Option->_preSave() in XenForo/DataWriter.php at line 1446
    [*]XenForo_DataWriter->preSave() in XenForo/DataWriter.php at line 1385
    [*]XenForo_DataWriter->save() in XenForo/Model/Option.php at line 572
    [*]XenForo_Model_Option->updateOptions() in XenForo/ControllerAdmin/Option.php at line 174
    [*]XenForo_ControllerAdmin_Option->actionSave() in XenForo/FrontController.php at line 347
    [*]XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 134
    [*]XenForo_FrontController->run() in /Users/tomchristian/Dropbox/www/xenforo1.3/admin.php at line 13
    Any ideas?
  14. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    I believe this is referring to your function signature:

    public static function validate($field, &$value, &$error)
    The second parameter (&$value) is expected to be a reference as shown above (& means reference). Make sure your function signature matches this.
  15. Tom Christian

    Tom Christian Active Member

    You're right. However, I've actually already solved this but thanks for replying :)
  16. ismooch

    ismooch Member

    Not trying to carry an on an old thread... but how would one pull the user id from this?
  17. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Try this:

    $visitor = XenForo_Visitor::getInstance();
    // now you can use $visitor['user_id']
  18. ismooch

    ismooch Member

    You have no idea how stupid I felt not thinking of that earlier. I was sure there was a reference somewhere I could get, and as much as I deal with outside PHP for Xen, this never occurred to me. So I have that working. Your ability to provide skeletons to some important things have alone helped my PHP knowledge boost our users' forum experience. However, I have but one more inquiry.

    I have been looking for ages and fail to see an example anywhere, of how to handle the Custom User Field display callback. The HTML is simple enough, but I do not see any examples(skeletons or otherwise) that could lead to the understand of how that callback works. Do you have any suggestions/examples on this? Sorry to bother you with something that probably seems trivial, but XenForo has always just done pretty much what I wanted to, and recently I have started trying to implement more and more of my own features into it, and this would help me greatly with a feature.
  19. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Display callback? Do you mean Value Display HTML when editing a custom field? That is just HTML with the defined tokens. Here is an example:

  20. ismooch

    ismooch Member

    I am aware of the HTML field, but Waindigo's Custom User Fields also adds a PHP callback for this section. I know you are not an addon developer so you may be the wrong person to ask for it. But, your skeletons have been super helpful to me so far, so I thought it would not be wrong to ask.

Share This Page