PHP
  Home arrow PHP arrow Page 2 - Controlling Online Forms with Builder Objects in PHP 5
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? 
Google.com  
PHP

Controlling Online Forms with Builder Objects in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    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:
      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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek