Making PHP Forms Object-Oriented - Inputs - Brief Introduction (Page 5 of 7 ) 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 a properly analysed 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 %LABEL'; var $separator = '<br>'; function RadioButtons($Aform, $AName, $OptionList, $AValue='', $AAttributes='', $AItemFormat='%RBTN %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, the 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. The name of input is then stored into a local data member. The 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. The 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 the definitions of the two other classes: TextInput and RadioButtons for an illustration of this. Next: Example of a Form >>
More PHP Articles More By Yuri Makassiouk |