Resource icon

Custom user field callback, validate value 1.x

No permission to download

Jake Bunce

Well-known 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.

This is a skeleton for creating a callback for a custom user field. The callback is used to validate the value entered by the user. For example, if you are collecting some kind of ID that you need to check against another database.

The callback is specified when editing the custom field:

View attachment 26184

The skeleton contains this file which matches the callback name:

library/Callback/UserField.php

The example code only accepts a value of '123'. Otherwise it defines an...

Read more about this resource...
 
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 :)
 
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 :)

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

http://xenforo.com/community/resources/run-query-on-user-upgrade-code-example.396/

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.
 
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):

f0ceaf3500143f180df55b967e5ed0aa.png


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!
 
That is different. You would have to extend the user datawriter:

XenForo_DataWriter_User::_verifyUsername
 
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.
 
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.

I would refer to my previous post:

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

http://xenforo.com/community/resources/run-query-on-user-upgrade-code-example.396/

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.

That would allow you to query the xf_user table in the database to see if the input value matches an existing username.
 
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:
PHP:
if ($value == $mytable['mycolumn'])

Same question above except when used in DataWriter_User:
PHP:
if ($this->get('xfpoints_promotion_code') == $promo['code'])

Or
PHP:
if($this->get('field', 'xfpoints_promotion_code') == $promo['code'])
 
@Mythotical

$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.
 
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.
 
What are you trying to do with it?
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.
 
@Jake Bunce I'm getting the following error when the callback returns false:

Code:
Server Error
Parameter 2 to Callback_UserField::validate() expected to be a reference, value given

[LIST=1]
[*]XenForo_Application::handlePhpError()
[*]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?
 
@Jake Bunce I'm getting the following error when the callback returns false:

Code:
Server Error
Parameter 2 to Callback_UserField::validate() expected to be a reference, value given

[LIST=1]
[*]XenForo_Application::handlePhpError()
[*]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?

I believe this is referring to your function signature:

Code:
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.
 
I believe this is referring to your function signature:

Code:
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.

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

Code:
$visitor = XenForo_Visitor::getInstance();

// now you can use $visitor['user_id']

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

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:

https://xenforo.com/community/threads/custom-user-field-check-box-w-image.33288/#post-379981
 
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:

https://xenforo.com/community/threads/custom-user-field-check-box-w-image.33288/#post-379981

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.
 
Back
Top Bottom