Page 1 of 1

call a function

Posted: Sat Feb 04, 2006 3:44 pm
by ed209
sorry for the vague title, not really sure how to sum this problem up.

I am making a class that creates and validates forms. On creating an input field in the form, I want to specify what validation function should be used for that field.

The validation check will probably be done by a function. That function will be defined in the class. I can't post the code as there's too much so I'll try and give an example.

My Class:

Code: Select all

<?php
class myFormClass{

//...rest of the class up here 

function vaildateEmail($email){
      if( this is an $email ) ? return true : return false;
}

}
?>
my form page where I make the form - and where the data posts back to (action is itself):

Code: Select all

<?php
$my_form = new myFormClass;

$my_form->makeAnInputField(various parameters here, array("vaildateEmail", "maybe another parameter..."));

?>

I want my data checker function in 'myFormClass' (not shown) to use the "vaildateEmail" function to check the data for that field. The only way my data checker function will know which function to use is because of this first element of the passed array.

I have considered using 'case' i.e. if the first element of the array == "vaildateEmail" then call that function. The reason I don't want to do that is because I just want to add new functions into my class as I need them without having to alter it in lots of places like switch statements.

Am I making any sense???
Thanks for the help in advance.

Posted: Sat Feb 04, 2006 6:58 pm
by feyd

Posted: Sat Feb 04, 2006 7:06 pm
by Christopher
What you really want is a rule based validation. It is probably the best solution and a clean OO one (but not mentioned here much):

Code: Select all

<?php
$my_form = new myFormClass;

$my_form->addRule('fieldname', new EmailRule ("error message if rule fails"));
?>
Rules usually inherit a base rule class. They look like:

Code: Select all

<?php
class MyRule {
    function MyRule($errmsg) {}
    function isValid($value) {}
}
?>

Posted: Sun Feb 05, 2006 5:15 am
by ed209
thanks for the help. I was looking for something like call_user_func(), I knew I had seen it somewhere but couldn't find it on php.net - didn't really know what to search for, I did stumble across is_callable() and get_defined_functions().

The rules look interesting though, I've never heard of them.

So would I create a validation class separate from my form making class and add functions to validate the submitted data, or would my form making functions and validation functions be better off in the same class?

I'm not really sure I understand how to implement it. In your example, is 'addRule' a function you have defined?

Posted: Mon Feb 06, 2006 9:15 am
by ed209
I've used call_user_func() with some success but I have encountered a problem which I thought migh be relevant to this post, basically, it's a problem with setting a class wide variable from a function called by 'call_user_func()'.

The post can be seen here:


viewtopic.php?p=235882#235882