Inheritance and Polymorphism in PHP: Building a Form Generator - Part II - Coming up: more subclasses
(Page 4 of 5 )
If you think that the above class definitions are not enough for building our form generator, you're right. There are still more form elements to be defined. So, it's time to continue defining more form subclasses. The next one to be defined is a <textarea> element, listed below:
class textAreaObject extends formObject {
var $value;
var $wrap;
function textAreaObject($label,$name,$style,$value,$wrap='virtual'){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
($wrap=='off'||$wrap=='physical'||$wrap=='virtual')?
$this->wrap=$wrap:die('Invalid parameter '.$wrap);
}
function generateHTML(){
$html=$this->label.'<textarea name="'.$this->name.'"
class="'.$this->style.'" wrap="'.$this-
>wrap.'">'.$this->value.'</textarea>';
return $html;
}
}
The above listed class for building a <textarea> element accepts two additional parameters: $value and $wrap, in order to use them for proper HTML generation. In this case, we've given a default value of "virtual" for the $wrap parameter.
Now let's look at the form button elements. Its class definition is the following:
class buttonObject extends formObject {
var $value;
function buttonObject($label,$name,$style,$value){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
}
function generateHTML(){
$html=$this->label.'<input type="button" name="'.$this-
>name.'" value="'.$this->value.'" class="'.$this-
>style.'" />';
return $html;
}
}
Our next step consists of listing the class for generating password fields. Here's the code:
class passwordObject extends formObject {
var $maxlength;
function passwordObject($label,$name,$style,$maxlength){
parent::formObject($label,$name,$style);
(is_int($maxlength)&&$maxlength>8&&$maxlenght<24)?
$this->maxlength=$maxlength:die('Invalid parameter
'.$maxlength);
}
function generateHTML(){
$html=$this->label.'<input type="password"
name="'.$this->name.'" value="'.$this->value.'"
class="'.$this->style.'" />';
return $html;
}
}
Okay, let's put in a bit more effort, and define the class for file uploads elements:
class fileObject extends formObject {
function fileObject($label,$name,$style){
parent::formObject($label,$name,$style);
}
function generateHTML(){
$html=$this->label.'<input type="file" name="'.$this-
>name.'" class="'.$this->style.'" />';
return $html;
}
}
Let's not forget to define the subclass for reset buttons. Here is the code:
class resetObject extends formObject {
var $value;
function resetObject($label,$name,$style,$value){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
}
function generateHTML(){
$html=$this->label.'<input type="reset" name="'.$this-
>name.'" value="'.$this->value.'" class="'.$this-
>style.'" />';
return $html;
}
}
Finally, we're going to show our last two classes: form image elements and submit buttons. First, here's the code for form image elements, often used for submitting forms without using the venerable submit button:
class imageObject extends formObject {
var $value;
var $src;
var $alt;
function imageObject
($label,$name,$style,$value,$src,$alt){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
(!is_numeric($alt))?$this->alt=$alt:die('Invalid
parameter '.$alt);
(file_exists($src))?$this->src=$src:die('Invalid
parameter '.$src);
}
function generateHTML(){
$html=$this->label.'<input type="image" name="'.$this-
>name.'" value="'.$this->value.'" class="'.$this-
>style.'" src="'.$this->src.'" alt="'.$this-
>alt.'" />';
return $html;
}
}
Finally, the class definition for a submit button:
class submitObject extends formObject {
var $value;
function submitObject($label,$name,$style,$value){
parent::formObject($label,$name,$style);
(!is_numeric($value))?$this->value=$value:die('Invalid
parameter '.$value);
}
function generateHTML(){
$html=$this->label.'<input type="submit" name="'.$this-
>name.'" value="'.$this->value.'" class="'.$this-
>style.'" />';
return $html;
}
}
I must give a short disclaimer: notice that in all of the subclasses that accept a $value parameter, we've opted not to consider as a valid argument any numeric value. While this validation process could be leaving out some potentially valid parameters, we've chosen to do this based on a data type checking rather than performing validation on data length. For particular cases, just implement the validation process you believe best suits your needs.
Now we have fully defined each subclass derived from the form base class for creating the form generator. Since subclasses nicely expose the "generateHTML()" method, it is easy to figure out a technique to put all of the subclasses to work and build a complete HTML form. So, let's try creating a regular form, using the above defined classes.
Next: Implementing the Form Generator: take one >>
More PHP Articles
More By Alejandro Gervasio