Dynamic form building / metaprogramming / PHP
I'm looking for design ideas about creating dynamic forms.
What I currently have is a bunch of models with a few properties / variables for example:
class Group extends IDKModel {
/**
* @Form
*/
public $title;
/**
* @Form(validation="{my validation rules here}")
*/
public $permissions;
}
In that example the Form PHPAnnotation defines it as a form element and the validation array will have built in validation rules in it. Now the problem I'm having is I have no idea how to implement conditionals.
For example, how to show $permissions only if a user is an admin. How to show $title if time of day is past 12:00 GMT. Basically any kind of conditional.
Taken from #php at irc.quakenet.org:
[10:50] <ramirez> i would not try to stick all of the meta-info under one attribute
[10:50] <ramirez> it'd be much cleaner to do something like
[10:50] <ramirez> @FormElement
[10:51] <ramirez> @Validator(params)开发者_如何学Go
[10:51] <ramirez> etc
[10:52] <ramirez> anyways, I would probably do something like.. @Filter(name="Group",value="admin,editor")
[10:53] <ramirez> then for each filter you want to implement, you'll create a class like "Model_Filter_Group", which would be used for eg. the above filter
[10:53] <ramirez> that class in this case would simply explode the groups by comma and see if user is in any of those groups
[10:54] <ramirez> you can use that for any kind of filtering, eg: @Filter(name="PastTime", value="12:00")
Anyone have a simpler idea?
I suggest you take a look to Zend_Form component in Zend Framework: Zend Framework Reference:Zend_Form
Zend_Form is very powerful and highly customizable but can be a bit too complex to see all of its capabilities provided by good OOP design.
Even if ZF of no use for you, you can find good ideas there.
btw: manual covers only basics as well as tutorials found on the web
精彩评论