sennetta Posted May 22, 2010 Share Posted May 22, 2010 Hey there. I'm new here. I'm building an abstract form generation/validation class, which is to be subclassed for each different form, and it's time to develop the data validation routines. What I'd like to do is similar to Yii's way of defining the data model through a function of a Model class, which returns an array of validation flags, but instead of passing a load of different flags, I'd like to pass the validation function itself. PHP can do anonymous functions, and pass them as a variable, but so far I have not have any success passing an anonymous function in an array (which after all is just a collection of variables). This gives a clearer picture, and also shows what I am trying at the moment. It is part of the child class of the abstract Form class: protected function defineFields() { return array( "testInputElement" => array( "type" => "InputElement", "elementOptions" => array( "errorMessage" => "Please input a whole number between 30 and 90", ), "validation" => function() { return false; } ), "testInputElement2" => array( "type" => "InputElement", "errorMessage" => "Error Message for form field 2 goes here", "elementOptions" => array(), "validation" => function() { return false; } ), ) ); } Note the attempt to define the array key 'validation' as an anonymous function. This throws a syntax error. After the parent abstract Form class receives this array of values (by calling defineFields() in the child class), I would like to assign the passed function as the validate function of the FormElement class, so that it can validate itself. I have tried defining the function elsewhere in script (both inside and outside the class), and passing it through the array as variable, but this also yields a parse error. I have also looked at create_function(), but I can't see how I would assign it as a class function to the FormElement class. Has anyone have any experience I could call upon? Obviously I could pass in a load of "int", "max" => 200, "min" => 100", but this would take a lot more work - it's easier and more extensible just to define one function per form field. I've kept the above as concise as possible for briefness, but please tell me if there is not enough info or it makes no sense, and I'll elaborate. Many thanks, sennetta Quote Link to comment https://forums.phpfreaks.com/topic/202610-pass-anonymous-function-in-array/ Share on other sites More sharing options...
Daniel0 Posted May 22, 2010 Share Posted May 22, 2010 The syntax error you're getting has nothing to do with the anonymous functions. Try matching up the opening parentheses with the closing parentheses. You have one more closing parenthesis than opening ones. Quote Link to comment https://forums.phpfreaks.com/topic/202610-pass-anonymous-function-in-array/#findComment-1062053 Share on other sites More sharing options...
sennetta Posted May 22, 2010 Author Share Posted May 22, 2010 Thanks for responding. Yes cheers for that I didn't see it. So the code now looks like this: protected function defineFields() { return array( "testInputElement" => array( "type" => "InputElement", "errorMessage" => "Please input a whole number between 30 and 90", "elementOptions" => array(), "validation" => function() {return false;} ), "testInputElement2" => array( "type" => "InputElement", "errorMessage" => "Error Message for form field 2 goes here", "elementOptions" => array(), "validation" => function() {return false;} ) ); } And it's still giving a parse error. Is there anything else I've missed? Quote Link to comment https://forums.phpfreaks.com/topic/202610-pass-anonymous-function-in-array/#findComment-1062058 Share on other sites More sharing options...
Daniel0 Posted May 22, 2010 Share Posted May 22, 2010 <?php $array = array( "testInputElement" => array( "type" => "InputElement", "errorMessage" => "Please input a whole number between 30 and 90", "elementOptions" => array(), "validation" => function() {return false;} ), "testInputElement2" => array( "type" => "InputElement", "errorMessage" => "Error Message for form field 2 goes here", "elementOptions" => array(), "validation" => function() {return false;} ) ); var_dump($array); Gives no parse errors for me. Maybe you would like to enlighten us with what error you're getting? Quote Link to comment https://forums.phpfreaks.com/topic/202610-pass-anonymous-function-in-array/#findComment-1062059 Share on other sites More sharing options...
sennetta Posted May 22, 2010 Author Share Posted May 22, 2010 Thanks. Yes the actual message itself might have been a good idea. Sorry about that :S Running your code gives me the following: Parse error: syntax error, unexpected T_FUNCTION in D:\www\form\danielO.php on line 7 Quote Link to comment https://forums.phpfreaks.com/topic/202610-pass-anonymous-function-in-array/#findComment-1062063 Share on other sites More sharing options...
Daniel0 Posted May 22, 2010 Share Posted May 22, 2010 Which version of PHP are you using? Anonymous functions were added in PHP 5.3.0. If you're using an earlier version you'll have to upgrade. Quote Link to comment https://forums.phpfreaks.com/topic/202610-pass-anonymous-function-in-array/#findComment-1062065 Share on other sites More sharing options...
sennetta Posted May 22, 2010 Author Share Posted May 22, 2010 5.2.3. Damn do I feel thick. Thanks very much for your time mate appreciate it. All the best. Quote Link to comment https://forums.phpfreaks.com/topic/202610-pass-anonymous-function-in-array/#findComment-1062066 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.