PHPBuilder - Making forms object-oriented Page 6



RSS Twitter
Articles Application Architecture

Making forms object-oriented - Page 6

by: Yuri Makasjuk
|
October 15, 2001

Inputs - brief introduction

In fact, auto-assigning inputs' names really was the initial idea that pushed me towards developing of another set of classes. The classes that would serve as object wrappers for HTML form's input controls. In this article I would really like us to concentrate on the FormProcessor class and its descendants. Developing of a properly analyzed hierarchy of classes for form controls deserves a separate discussion. So, I suggest that we only talk about form controls classes as much as we need for completing this study.

<?php

  
/**
  * A small hierarchy of classes that 
  * serve as object wrappers for HTML form's inputs
  */

  /**
  * An abstract class representing generic form control
  *
  * @author Yuri Makassiouk <racer@bfpg.ru>, 
  *  Mission & Media <firma@missionandmedia.com>
  */
  
class FormControl {
    var 
$Name;
    var 
$form;
    var 
$Value;
    var 
$Attributes;
    var 
$FormName;
    var 
$InputName;
    
    function 
FormControl($Aform$AName$AValue=''$AAttributes='') {
      
$this->Name $AName;
      
$this->form $Aform;
      
$this->Value 
        (
$this->form->FirstTime)?$AValue:($this->form->Values[$this->Name]);
      
$this->Attributes $AAttributes;
      
$this->FormName $Aform->Name;
      
$this->InputName sprintf("%s%s[%s]"
        
$this->form->fieldPrefix$this->FormName$this->Name);

      
$this->Display();
    }
    
    function 
InputName() {
      echo 
$this->InputName;
    }
    
    function 
Display() {
      echo 
$this->Render();
    }
    
    function 
Render() {}
    
//abstract
  
}
  
  
/**
  * Class representing a text control
  * 
  */
  
class TextInput extends FormControl {

    function 
Render() {
      return 
"<input type=\"Text\" name=\"".
        
$this->InputName."\" value=\"$this->Value\" $this->Attributes>";
    }
  }

  
/**
  * Class representing a set of radio buttons
  * 
  */
  
class RadioButtons extends FormControl {
    var 
$options;
    var 
$ItemFormat '%RBTN&nbsp;%LABEL';
    var 
$separator '<br>';
    
    function 
RadioButtons($Aform$AName
        
$OptionList$AValue=''$AAttributes=''
        
$AItemFormat='%RBTN&nbsp;%LABEL'$Aseparator '<br>') {
      
$this->options $OptionList;
      
$this->ItemFormat $AItemFormat;
      
$this->separator $Aseparator;
      
$this->FormControl($Aform$AName$AValue$AAttributes);
    }
    

    function 
Render() {
      
$i=0;
      
$out '';
      while (list(
$key$val)=each($this->options)) {
        
$item $this->ItemFormat;
        
$item str_replace('%RBTN'
            
sprintf("<input type=\"Radio\" name=\"%s\" 
            value=\"${key}\" $this->Attributes%s>"

            
$this->InputName$key==$this->Value?' checked':''), $item);
        
$item str_replace('%LABEL'$val$item);
        if (++
$i!=count($this->options))
          
$item .= $this->separator;
        
$out .= $item;
      }
      return 
$out;
    }
  }

?>
As you can see, constructor of the base class (FormControl) does all the job of linking the control to form object, reference to which is passed as one of parameters. Then, the name of HTML input is evaluated. It should be such that the value will be submitted as part of an array which the form (descendant of FormProcessor) will be looking for. Name of input is then stored into a local data member. Control also receives the default value as one of constructor's parameters. This value will be used if the form that contains the control is displayed for the first time. Control is accessing a data member of form class to check this. Otherwise, the default value will be taken from the form's $Values array.
Then the constructor calls the Display() member function, which echoes output of the Render() function. This function is abstract and should be overridden in sub-classes so that they produce correct HTML code to represent themselves in pages. See definitions of the two other classes: TextInput and RadioButtons for illustration of this.

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Yuri Makasjuk

Comment:



Comment:

(Maximum characters: 1200). You have characters left.