PHP
  Home arrow PHP arrow Page 2 - 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 - Refreshing some core concepts
    ( Page 2 of 4 )

    Before I proceed to creating the mentioned form director class that completes the schema imposed by the builder pattern, it's convenient to take a brief look at the classes that were defined in the previous article.

    Doing so, you'll have a fresh idea of how each of the components that implements the builder mechanism look. Keeping in mind this relevant concept, I'll start by listing the first two classes involved into the creation process, that is the pair responsible for generating different form elements. The definitions for these classes are as follows:

     

    // define abstract 'AbstractFormElement' class abstract class AbstractFormElement{ abstract function getHTML(); } // define concrete 'formElement' class class FormElement extends AbstractFormElement{ private $html=''; public function __construct($type='text',$attributes=array
    ('name'=>'default'),$options=array()){ // check for <input> elements if(preg_match("/^(text|radio|checkbox|password|hidden|submit|
    reset|button|image|file)$/",$type)){ $openTag='<input type="'.$type.'" '; $closeChar='';           $closeTag='/>'; } // check for <textarea> and <select> element else if(preg_match("/^(textarea|select)$/",$type)){           $openTag='<'.$type.' '; $closeChar='>';          $closeTag='</'.$type.'>'; } else{ throw new Exception('Invalid form element type'); }         if(!is_array($attributes)||count($attributes)<1){ throw new Exception('Invalid number of attributes for
    <'.type.'> element'); } // loop over element attribute        $elemAttributes='';         foreach($attributes as $attribute=>$value){ if(!$attribute||!$value){ throw new Exception('Invalid attribute or
    value for <'.type.'> element'); }            $elemAttributes.=$attribute.'="'.$value.'" '; } // check for <select> option $selOptions='';         if(count($options)>0){           foreach($options as $value=>$text){             if(!$value||!$text){ throw new Exception('Invalid value or text
    for <'.type.'> element'); }               $selOptions.='<option value="'.$value.'">'
    .$text.'</option>'; } } // build form element(X)HTML output         $this->html.=$openTag.trim($elemAttributes).$closeChar.
    $selOptions.$closeTag; } // return overall (X)HTML output public function getHTML(){< return $this->html; } }

     

    As shown above, these two classes represent the target element being created by the corresponding builder. In simple terms, the above "FormElement" class is tasked with rendering the different components that are found in typical online forms, such as input boxes, radio buttons, text areas, and so forth.

    Now, and getting rid for a moment of the handy features that come with the "FormElement" class, I need you to pay strong attention again to the definition of the builder pattern: it is two objects, the director and the builder respectively, that play a relevant role within the creation of a third object.

    In this case, since the target element is represented by all the form element objects eventually instantiated from the corresponding class, it's necessary to have at our disposal a builder class, which must be provided with the ability to create different form elements. At this moment, do you recall how all the objects that implement the builder pattern fit into the overall schema? I suppose you do.

    To keep fresh in your mind the definition of the pertinent "FormBuilder" class that was created in the previous article, during the following section I'll be taking a quick look at it. Therefore, if you need to recall how the aforementioned form builder class looked originally, please read the next few lines.



     
     
    >>> 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 6 Hosted by Hostway
    Stay green...Green IT