PHP
  Home arrow PHP arrow Page 3 - Developing a Form Director Class
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Developing a Form Director Class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2006-10-18


    Table of Contents:
  • Developing a Form Director Class
  • Refreshing some core concepts
  • The form builder class
  • Defining a form director class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Developing a Form Director Class - The form builder class
    ( Page 3 of 4 )

    In order to get the schema imposed by the builder pattern nearer to completion, it's convenient to recall quickly the signature of the respective "FormBuilder" class, along with its corresponding abstract parent.

    Below I have listed the full source code that corresponds to these two classes:

    // define abstract 'AbstractFormBuilder' class abstract class AbstractFormBuilder{ abstract function getForm(); } // define concrete 'FormBuilder' class class FormBuilder extends AbstractFormBuilder{ private $html=array(); private $action; private $method; public function __construct($action='',$method='post'){ // setup form attribute        $this->action=!$action?$_SERVER['PHP_SELF']:$action; $this->method=$method!='post'||$method!='get'?
    'post':$method; } // add form element public function addElement($type='text',$attributes=array
    ('name'=>'default'),$options=array()){ if(!$elem=new FormElement($type,$attributes,$options)){ throw new Exception
    ('Failed to instantiate '.$type.' object'); }       $this->html[]=$elem->getHTML(); } // add form part public function addFormPart($formPart='<br />'){         $this->html[]=trim($formPart)==''?'<br />':$formPart; } // get web form public function getForm(){       $formOutput='<form action="'.
    $this->action.'" method="'.$this->method.'">';        foreach($this->html as $html){          $formOutput.=$html; }         $formOutput.='</form>'; return $formOutput; } }

    All right, now I firmly believe that the two classes listed above are fresh in your mind. If they aren't yet, let me explain briefly how they fit into the builder pattern schema. As you'll see, the prior "FormBuilder" class is tasked with generating the whole online form, in accordance with some input parameters passed to its constructor, such as the $method and $action variables.

    In addition, the class also comes with the "addElement()" method, which is pretty handy for adding different form elements to the general structure. In this way it allows for the dynamic generation of regular online forms. Of course, the remaining class methods speak for themselves, therefore I'll skip rapidly over them.

    At this stage, and based on the set of classes that were listed earlier, the schema established by the pattern builder is very close to completion. After all, I have on one hand a "FormElement" class (in this case referenced as the target element) that obviously renders different form controls, while on the other hand there's the form builder class that you saw before, responsible for using the mentioned form elements to create the complete online form.

    So, the question that comes up now is, what's the missing piece here? Obviously, the element that remains undefined is a form director class, which will instruct to the form builder on how the online form in question must be constructed. Indeed, that makes a lot of sense!

    Regarding the definition of the corresponding form director class, in the upcoming section I'll show you its complete signature. If you want to see how this brand new class looks, please keep reading.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT