The Singleton and Factory Patterns in PHP: Building a Form Generator Class - Object-based form generation (continued): explaining the remaining class methods (
Page 2 of 4 )
As I said before, we need to take a detailed look at the rest of the class methods, so if you’re accustomed to coding web forms on a regular basis, they should be fairly easy to understand.
The “addFormPart()” method is tasked with including any required additional markup that will be rendered outside the iteration for displaying form elements. Say that some tags need to be added to the structure of the form for achieving a better layout. In this case, the method will append the required markup to the overall form’s code, as shown below:
// add form part
public function addFormPart($html='<br />'){
$this->output.=$html;
}
As you can see, I’ve specified a default “<br />” tag to be appended when the method is invoked, but this might be easily modified to accept any other required input tag (or a set of tags).
The other two methods related to including additional markup within the iteration process for displaying form elements are “addElementHeader()” and “addElementFooter()”. Despite the fact they were mentioned previously, here are their respective definitions:
// add element header
public function addElementHeader($header){
$this->elementHeader=$header;
}
// add element footer
public function addElementFooter($footer){
$this->elementFooter=$footer;
}
Similar to “addFormPart()”, the above methods are useful for inserting interspersed markup before and after the code of a form element that has been appended to the form’s output. As expected, these methods implement a simple yet effective way to wrap form fields into regular containers such as <div>, <p> or <table> elements.
Having explained the core methods of the class, as well as those aimed at adding presentational functionality, the remaining methods are the respective modifiers for the “$name”, “$action” and “$method” properties. They look like this:
// set form name
public function setName($name){
$this->name=$name;
}
// set form action
public function setAction($action){
$this->action=$action;
}
// set form method
public function setMethod($method){
if($method!='post'&&$method!='get'){
throw new Exception('Invalid form method');
}
$this->method=$method;
}
Because of their extreme simplicity, the above listed methods shouldn’t be difficult to understand, since they’re tasked with setting the values for each form property I mentioned before. Probably, the “setAction()” method might be boosted by checking whether the “$action” parameter is a valid file, but additional features will be left as possible class improvements.
Finally, the last method to be listed is “getFormCode()”, which simply returns the complete form’s output, to be displayed accordingly:
// get dynamic form output
public function getFormCode(){
return $this->output;
}
With all of the methods covered in detail, the next thing to do is set up a practical example, in order to test the capabilities of the recently developed “formGenerator” class and see the real power of design patterns.
| | 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! | | | | | |
|
 |