[SOLVED] ValidationServerFunction in forms class trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tripps
Forum Newbie
Posts: 9
Joined: Mon Jul 26, 2004 3:28 pm

[SOLVED] ValidationServerFunction in forms class trouble

Post by tripps »

I have a class which I use with all my database user related functions. Within that class I have a UserExists() function, which is passed the username and returns true if the user exists and false if not. The class encapsulates all the database calls, etc., so I don't have to fool with it. So, I've created a wrapper function which essentially reverses the return to work with the ValidationServerFunction. For example:

Code: Select all

$registration->AddInput(array(
	"TYPE"=>"text",
	"NAME"=>"username",
	"ID"=>"username",
	"TABINDEX"=>1,
	"MAXLENGTH"=>20,
	"SIZE"=>40,
	"ValidateRegularExpression"=>"^[a-zA-Z0-9]+$",
	"ValidateRegularExpressionErrorMessage"=>"The username may only contain letters and digits.",
	"ValidateAsNotEmpty"=>1,
	"ValidateAsNotEmptyErrorMessage"=>"Please enter a valid username.",
	"ValidateMinimumLength"=>3,
	"ValidateMinimumLengthErrorMessage"=>"Your username must be at least 3 characters long.",
	"LABEL"=>$registrationfields['username'],
	"ValidationServerFunction"=>"UserIsUnique",
	"ValidationServerFunctionErrorMessage"=>"That username already is being used. Please choose another username.",
	"ValidateOnlyOnServerSide"=>1
));
Here is the UserIsUnique function:

Code: Select all

require("myclasslib.php");
$myclasslib = new MyClassLib();

function UserIsUnique($username)
{
	$return = $myclasslib->UserExists($username);
	if ($return == 0)
		return true;
	else
		return false;
}
The problem is I keep getting the "Fatal error: Call to a member function on a non-object in xxxx" error message referring to the line which contains the $myclasslib->UserExists call. I clearly initialize the class How do I fix this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this is a variable scope problem. $myclasslib is in the global space, while the $myclasslib in the function is in the function's local space. Adding

Code: Select all

global $myclasslib;
to your function should correct this error.
tripps
Forum Newbie
Posts: 9
Joined: Mon Jul 26, 2004 3:28 pm

Post by tripps »

That's the first thing I tried and it didn't work. I'll give it another shot though.
feyd wrote:this is a variable scope problem. $myclasslib is in the global space, while the $myclasslib in the function is in the function's local space. Adding

Code: Select all

global $myclasslib;
to your function should correct this error.
tripps
Forum Newbie
Posts: 9
Joined: Mon Jul 26, 2004 3:28 pm

Post by tripps »

That did the trick. Thanks! I put the global in the wrong place. ;)
Post Reply