PHP
  Home arrow PHP arrow Page 2 - Controlling Online Forms with Builder ...
Dev Shed Forums 
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

Controlling Online Forms with Builder Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-10-11

    Table of Contents:
  • Controlling Online Forms with Builder Objects in PHP 5
  • Defining a form element class
  • Creating a form builder class
  • Constructing a basic online form

  • 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


    Controlling Online Forms with Builder Objects in PHP 5 - Defining a form element class


    (Page 2 of 4 )

    In order to start demonstrating how the builder pattern can be used for constructing online forms, first I need to define a target object that can be "manipulated" by the corresponding director and builder objects.

    First I'll define an abstract "FormElement" class and then I'll derive a child from it, which will offer a concrete implementation for all its abstract methods. That said, here is the initial definition of the "AbstractFormElement" class:

    // define abstract 'AbstractFormElement' class abstract class AbstractFormElement{ abstract function getHTML(); }

     

    If you were thinking that creating an abstract form element class with PHP was a hard-to-perform process, then I believe you'll find that you're wrong. As usually, the previous class defines the generic structure of a typical form control, and also presents an abstract method called "getHTML()," which will be responsible for returning to calling code the appropriate (X)HTML markup that corresponds to the online form in question. As you can see, this is nothing unexpected.

    Now, let me take one step forward and create a child class from the "AbstractFormElement" class defined earlier. The signature of this new sub class is listed below, thus examine its source code:

    // 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> elements 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 attributes $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> options $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 illustrated above, now the new child "FormElement" class offers a concrete implementation for each of the methods declared in the base class. Of course, the most important thing to note here concerns all the tasks performed inside the constructor.

    As you can appreciate, this method really does all the hard work required for creating the (X)HTML markup that corresponds to each element of a regular online form, based on the pair of arrays supplied as input arguments. Finally, the respective form's code is retrieved by the tight "getHTML()" method. Simple and powerful!

    Well, now that you know how the target object (in this case referenced by the "FormElement" class) has been correctly defined, the next step consists of creating the corresponding builder class, in this way introducing the second component required by the builder pattern (remember that the third one is the director). 

    In the next section I'll be defining the mentioned builder class, therefore I recommend you click on the below link and keep on reading. See you there.

    More PHP Articles
    More By Alejandro Gervasio


       · Over this final article of this series, the builder pattern is applied, in order to...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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