PHPBuilder - Making forms object-oriented Page 4



RSS Twitter
Articles Application Architecture

Making forms object-oriented - Page 4

by: Yuri Makasjuk
|
October 15, 2001

Implementing the FormProcessor class

But this was still quite far from our original subject, we were talking about object-oriented forms here, remember? Let's have a look at FormProcessor and see what's under its hood. I had to strip the code of PHPDoc-style comments, because I think it requires step-by-step explanations, while reading through the comments in arbitrary order might confuse. Version of this source with PHPDoc comments is available for download from here.

<?php

  
/**
  * An abstract class implementing generic functionality for processing user's input
  *
  * This class encapsulates generic functions for working 
  * with data coming from user forms. Descendants must only override certain 
  * functions that perform context-specific tasks, like custom checking of 
  * data, storing correct data, etc.
  *
  * @author Yuri Makassiouk <racer@bfpg.ru>, 
  *  Mission & Media <firma@missionandmedia.com>
  */
  
class FormProcessor {
    var 
$Name;
    var 
$FirstTime;
    var 
$Values;
    var 
$Errors = array();
    var 
$ErrorMessageFormat '<br>%s';
    var 
$wizardPage;
    var 
$fieldPrefix 'mm_form_data_';

    function 
FormProcessor($Name$wPage='') {
      
$this->Name $Name;
      
$this->wizardPage $wPage;
      
      
$this->Values $GLOBALS[$this->fieldPrefix.$this->Name];
      
$this->FirstTime = (count($this->Values) == 0);

      if (!
$this->FirstTime)
        
$this->CustomCheck();
        
      if (
$this->IsCompleted()) {
        
$this->StoreData();
        
$GLOBALS['wizardPage'] = $this->NextWizardPage();
      } 
      else
        
$this->DisplayForm();
    }
    
    function 
IsCompleted() {
      return (!
$this->FirstTime && count($this->Errors)<=0);
    }
    
    function 
CustomCheck() {}
    
//abstract
    
    
function DisplayForm() {}
    
//abstract
    
    
function NextWizardPage() {}
    
//abstract
    
    
function StoreData() {}
    
//abstract
    
    
function Additional() {
      if (
$this->wizardPage) :
    
?>
    <input type="Hidden" name="wizardPage" value="<?php echo $this->wizardPage?>">
    <?php endif;
    }
    
    function 
Set($Name$Value) {
      
$this->$Name $Value;
    }
    
    function 
ErrorReport($Name) {
      if (isset(
$this->Errors[$Name]))
        
printf($this->ErrorMessageFormat$this->Errors[$Name]);
    }
    
    function 
GetInitialValue($Name) {
      if (isset(
$this->Values[$Name]))
        return 
$this->Values[$Name];
      else
        return 
false;
    }
    
    function 
InitialValue($Name) {
      echo 
$this->GetInitialValue($Name);
    }
  }

?>

« 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.