Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. The Singleton and Factory Patterns in PHP: Building a Form Generator Class
  2. Object-based form generation (continued): explaining the remaining class methods
  3. A practical example: putting the “formGenerator” class to the test
  4. The source code box: listing the full developed classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
December 07, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: