HomePHP Page 2 - The Singleton and Factory Patterns in PHP: Building a Form Generator Class
Object-based form generation (continued): explaining the remaining class methods - PHP
In this final part of the series, Alejandro Gervasio examines a point that he has not taking into consideration so far: that the layout of form elements plays a relevant role within the overall development process. With this in mind, he encapsulates the logic needed to generate web forms by defining a form generator class. This class will implement the form element factory along with all of the required form element classes.
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.