A natural place to start building a form element factory is by coding the required classes to render each element. Maybe this sounds like common sense, but in fact it's a logical starting point within the development process. As you'd expect, writing the code for each form element doesn't present major difficulties due to its simplicity, but since we're dealing with many components, the task will be rather long.
A brief disclaimer is needed here: it's often a better solution to define a base class that contains the methods and properties shared by any form element, and then derive the required specific subclasses. However, in this case I won't stick to this method, simply to make the code a little more abbreviated and demonstrate the polymorphism of objects. Anyway, by the end of this series you'll have the knowledge to implement your own version of the form application.
Having said that, let's list the class source code for each form element. The first class renders a text input box, and looks like this:
// class textinput class textinput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="text" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } }
As I explained in the previous article, the above class generates the (X)HTML markup to display a regular input text box, which is returned by the "getHTML()" method. Let's move on to listing the class responsible for building password fields:
// class passwordinput class passwordinput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="password" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } }
Next, here is the list for hidden fields, file upload elements and submit images:
// class hiddeninput class hiddeninput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="hidden" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } }
// class fileinput class fileinput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="file" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } }
// class imageinput class imageinput{ private $html; public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception ('Invalid number of attributes'); } $this->html='<input type="image" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } public function getHTML(){ return $this->html; } }
Okay, sit tight just a little longer and jump into the next few lines. As you might guess, I'll show the remaining classes to get the form element classes completed.