blueman378 Posted December 9, 2008 Share Posted December 9, 2008 Hi guys, Well im making a form generator for my personal use so its customized to how i work, anyway i was wondering if there is any way to make something like this you have a class called formgenerator you define $form1 = new formgenerator; inside formgenerator you have a function called addelement now heres wher it gets tricky you define what element you want to add eg select and then you define the parameters based off that so something like $form1->addelement("select(variables specific to select only here)"); so for example $form1->addelement("select('countries', 'countrylist.php', '5', true)"); which is name, fillscript, size, multiselect but then for a textbox $form1->addelement("textbox('mytextbox', 'type something here', '25', '50', 'alpha')"); which is name, value, size, maxlength, validation type any ideas how i could acomplish this as i dont want to have to call eg addselect(), addtextbox() addtextarea() Link to comment https://forums.phpfreaks.com/topic/136179-custom-function-patterns/ Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 You could have separate classes.. A form class, and then a class for each individual form element which you extend off of a base element class (or interface). This base element class could contain, among other things, an abstract function through which the form class can retrieve the html required to generate the element. Something like this... <?php $form = new Form('post', 'mypage.php', 'whateverelseyouwant'); $textarea = new Textarea('mytextarea', 'whateverelseyouwant'); $input = new Input('myinput', 'whateverelseyouwant'); $form->attach($textarea); $form->attach($input); $form->render(); // form can call the render() (or whatever you want to name it) function the //attached elements implemented because of their abstract parent to retrieve their html and display it ?> I realize this isn't too clear, but look up polymorphism and ask me if you'd like me to show you an actual implementation of this. Link to comment https://forums.phpfreaks.com/topic/136179-custom-function-patterns/#findComment-710356 Share on other sites More sharing options...
blueman378 Posted December 9, 2008 Author Share Posted December 9, 2008 i see what your saying sounds like a good idea, although id like to keep it in as little files as possible. what ive come up with is basically have addelement() with a whole pile of paramters named $param1 $param2 $param 3 ect they all have the false attribute by default and i have as many as i will need at maximum, then i have a switch which checks what the first attribute it branches off into the different code for each element so texbox might read param1 as valu but select might read it as fill. that seems to be working but now im at a part where i ask a completly different question. the code makes a multidimetional array called elements it looks like this: Array ( [0] => Array ( [Type] => select [Name] => myname1 [size] => mysize [Fill] => myfill [Multi] => no ) [1] => Array ( [Type] => select [Name] => myname2 [size] => mysize [Fill] => myfill [Multi] => no ) [2] => Array ( [Type] => text [Name] => mytextbox [size] => 30 [Maxl] => 50 [Value] => -no- [Val] => alphanum ) [3] => Array ( [Type] => password [Name] => mypassword [size] => 30 [Maxl] => 50 [Value] => -no- [Val] => alphanum ) ) anyway how would i go about running a code on each second level array to make eg the password one <input type="password" name="mypassword" size="30" maxlength="50" value=""> (i would handle carrying the val part seperatly im thinking i will need something like <?php foreach ($this->elements as $value) { echo "<input "; foreach ($value as $paramater => $value2) { if($value2 != "-no-") { echo "$paramater=\"$value2\" "; } } echo ">\n"; } ?> which would work for the text boxes/ password style things (i would make the others later) but is there anyway to do this cleaner? Link to comment https://forums.phpfreaks.com/topic/136179-custom-function-patterns/#findComment-710380 Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 Like I said, doing it the way I suggested. Object oriented programming is all about encapsulation, polymorphism and inheritance. Design consists of isolating what differs and visualizing what objects exist in the system. Form elements aren't a form, they're in a form, and they differ wildly. It's not the form's job to know how the form elements illustrate themselves through HTML, it's the form elements themselves. Link to comment https://forums.phpfreaks.com/topic/136179-custom-function-patterns/#findComment-710767 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.