PHP
  Home arrow PHP arrow Page 2 - 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 - 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


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