Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/202610-pass-anonymous-function-in-array/
Share on other sites

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?

<?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? :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.