Home arrow PHP arrow Page 2 - Controlling Online Forms with Builder Objects in PHP 5

Defining a form element class - PHP

Mastering some of the most popular design patterns with PHP 5 can be sometimes an overwhelming process that requires hard work and experience. However, if you want to tackle the challenge and expand your existing background on them, this article might eventually find a place on your quick reference list.

TABLE OF CONTENTS:
  1. Controlling Online Forms with Builder Objects in PHP 5
  2. Defining a form element class
  3. Creating a form builder class
  4. Constructing a basic online form
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
October 11, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: