Page 1 of 1

[SOLVED] ValidationServerFunction in forms class trouble

Posted: Tue Jul 27, 2004 6:06 pm
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?

Posted: Tue Jul 27, 2004 6:43 pm
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.

Posted: Wed Jul 28, 2004 9:13 am
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.

Posted: Wed Jul 28, 2004 11:08 am
by tripps
That did the trick. Thanks! I put the global in the wrong place. ;)