Forum Moderators: coopster
I had this idea about form validation using variable Functions, but with a twist.
I need to do something like this:
<?php
function foo($arg = '')
{
echo "In foo(); argument was '$arg'.<br />\n";
}
$func = 'foo';
$func('test'); // This calls foo() with the argument 'test'
?>
But where I pass in a default argument when I declare my variable, something like this:
<?php
function foo($arg1 ='', $arg2 = '')
{
echo "In foo(); argument 1 was '$arg1' and argument 2 was '$arg2'.<br />\n";
}
$func = 'foo("my first argument")'; // This part DOES not work
$func('test'); // This calls foo() with the first argument "my first argument" and second argument 'test'
?>
The idea being that create an array to validate my input, where the array keys are the field names, and the value is the function to call to validate that particular field value.
So I don't have to create one function for each field, I want to have one function to validate using a regular expression, that I type in at design time (in that validation array).
Can this be done with variable functions? If so how?
[edited by: Vali at 10:17 pm (utc) on Feb. 28, 2008]
$func = 'foo("my first argument")'; // This part DOES not work function foo("my first argument")($arg1 = '', $arg2 = '') { I think I understand where you may be headed with this, but can you show a bit more information such as how your intended array may be structured?
Well, thanks for the reply, but I know that the line does not work :p
It was just meant to show what I needed (add the function name + a parameter to that variable, in one string)
My array would look like this (note, does not work, it's just for the sake of giving an example of the idea):
$validationData[fomnField1] = "normal_regex('/^[1-9]+$/')";
$validationData[fomnField2] = "normal_regex('/^[a-z]+$/')";
$validationData[fomnField3] = "special_validation";
$validationData[fomnField4] = 1;
The idea was to validate the fields like this:
foreach ($_POST as $key => $value) {
.. if (issset($validationData[$key])) {
.... if (!validationData[$key]($value)) {
...... $error .= "You have an error with the $key field.<br />";
.... }
.. }
}
So far, the only way I found of doing it, is to make the $validationData array be a multidimensional array, that will have the function name in index 0, and the parameters after that.
But that makes my simple validation code a bit bigger, slower, and the config file with the validation rules gets a bit more complex.
I was looking for a simpler way to validate the user input.
Any ideas?
but I know that the line does not work
I just copied/pasted your code. I was explaining why it did not work, for clarification for you and also for future readers.
Have you considered passing the entire $validation array to the class (or function) and loop and process it within there rather than loop through and pass arguments to the class/function for processing?
$test = array(
'text' => '%[a-z\s\.,\?]+%i',
'number' => '%[0-9]+%'
);
function validate($input, $type) {
global $test;
if(preg_match($test[$type], $input)) {
$out = 'This is ok';
}
else {
$out = 'This is not ok';
}
}