The Singleton and Factory Patterns in PHP: Building a Form Generator Class - A practical example: putting the “formGenerator” class to the test (
Page 3 of 4 )
Despite the rather complicated-sounding nature of the phrase, building object-based forms is a really easy thing to do. With all of the form element classes already defined, as well as the “formElementFactory” class acting as a real factory that returns form objects, setting up the “formGenerator” class for displaying layout-controlled web forms is a pretty straightforward task.
To test the functionality of the “formGenerator” class, I’ll show an example that builds a simple login form, comprised of “username” and “password” fields, together with the corresponding login button. Also, I’ll add some additional markup within the form, to demonstrate the control over the general layout that the class offers. So, the sample code looks like this:
// define parameters for the formGenerator class
$formElements=array('textinput'=>array
('name'=>'username','maxlength'=>'20'),'passwordinput'=>array
('name'=>'password','maxlength'=>'20'),'submitbutton'=>array
('name'=>'login','value'=>'Log in'));
// instantiate a new formGenerator object
$fg=new formGenerator($formElements);
// add element labels
$fg->addFormPart('<table style="float:left;"><tr><td>User
Name<td></tr><tr><td>Password<td></tr></table>');
// add a table to the form code
$fg->addFormPart('<table>');
// add a table row as element header
$fg->addElementHeader('<tr><td>');
// add closing tags
$fg->addElementFooter('</td></tr>');
// generate form
$fg->createForm();
// add a closing </table> tag
$fg->addFormPart('</table>');
// display the form
echo $fg->getFormCode();
As you can see, although the above snippet takes up only a few lines of code, it is actually very powerful. First, the script builds a recursive array with the name and parameters of each form object to be created. Here is the line responsible for doing this:
// define parameters for the formGenerator class
$formElements=array('textinput'=>array
('name'=>'username','maxlength'=>'20'),'passwordinput'=>array
('name'=>'password','maxlength'=>'20'),'submitbutton'=>array
('name'=>'login','value'=>'Log in'));
Then, a new “formGenerator” object is instantiated and the above array is passed to the constructor. Let’s stop briefly at this point and see what’s happening here: if we return to the definition of the class, it’s clear that only two lines are required to generate the main source code of the form, because all of the internal processing for factoring form objects is handled inside the class context. This simple concept demonstrates the real power of the factory pattern, when correctly applied.
The rest of the code uses the “addFormPart()” method to roughly build a table containing the labels for each field, along with the “addElementHeader()” and “addElementFooter()” methods to wrap up form fields into a containing <table> structure.
Finally, the complete (X)HTML markup is displayed by calling the “getFormCode()” method, like this:
echo $fg->getFormCode();
By this point, you’ve been provided with the required background to use some design patterns in order to build web forms based on an object-oriented method. Definitely, repetitive and boring tasks involved in the development of web programs such as form generation, can be easily turned into flexible processes by conceiving them as an issue feasible to be solved through the object-oriented paradigm.
To wrap up, for those developers who want to have available “in one place” all the source code developed for PHP5, I’ll offer a list of each class used to build object-based forms.
| | Discuss The Singleton and Factory Patterns in PHP: Building a Form Generator Class | | | | | | | The final part of this series goes trough the makings of a form generator class, by... | | | | | | Just wondering how you would have a web-designer and a developer team-develop the... | | | | | | Well, thank you for the comments on my article, since that proves it was quite... | | | | | | Widgets would be my suggestion as well. Just ensuring that those less versed in... | | | | | | Thanks to you for the quick and positive response! Also, I'm glad to know that you... | | | | | | Excellent write up Alejandro. May I ask for the reason this last part is written for... | | | | | | Hello Matthijs,
Thank you for the compliments on this PHP article series. It's... | | | | | | Hi Alejandro,
Thanks for your reply. Your suggestion did solve the problem I had!... | | | | | |
Hi Matthijs,
Good to know that your problem is now fixed up. Of course it's... | | | | | | That's really helpful Alejandro. I'll take your suggestions and build something. I... | | | | | | Thank you Matthijs. I'm happy to know that my suggestions were helpful to you.
My... | | | | | | Hi Alejandro,
Thinking about patterns, and comparing your earlier articles about... | | | | | | Hello Matthijs,
Well, it's not a hard question, but perphaps it's a little long... | | | | | | Your comments are certainly helpful. And thanks for the links. I did see the site... | | | | | | Hi, Mathjis. It's good to know that my answers were useful to you, as well as the... | | | | | | Thanks for a great article it's gone along way in selling the benefits of OOP in... | | | | | | Thank you for the kind comments on my article. I really appreciate your feedback.... | | | | | | Many thanks for the quick reply Alejandro. I'll give that a go - it should give some... | | | | | | Thanks - worked a treat. I might try using this pattern for field validation as... | | | | | | Thank you. Also, I'm glad to know that this form generator class has been useful to... | | | | | | Thank you for the comments. Yes, adding form validation routines would be a nice... | | | | | | Your form generator example must be excellent, as it was very straightforward for me... | | | | | | Hello - Your form script is great - I'm just wondering how you would include a users... | | | | | | Thank you I was having the same issue with this code. | | | | | | Hi Regan,
Thanks for the kind words on my PHP article. There’s a few simple ways... | | | | | | Hi Alejandro,
I am using your form generator class for the project I am working... | | | | | | Hi Alejandro,
First, I have to thank you for the quick reply! Based on your form... | | | | | | Hello Many,
Thanks for the comments. I already answered this post to your email... | | | | | | Hello again Many,
Thanks for the comments and it's good to know you solved your... | | | | | | >>> Post your comment now! | | | | | |
|
 |