PHP
  Home arrow PHP arrow Page 3 - Developing a Form Director Class
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · In this final part of the series, you'll learn how to build a form director class,...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT