Search:

CWIS Developers Documentation

  • Main Page
  • Classes
  • Files
  • File List
  • File Members

FormField.php

Go to the documentation of this file.
00001 <?PHP
00002 
00006 class FormField {
00007 
00008     # ---- PUBLIC INTERFACE --------------------------------------------------
00009 
00012 
00023     function FormField($Name, $IsRequired, $Label, $ValidFunc, $ValidMsgs)
00024     {
00025         # save field info
00026         $this->MyName = $Name;
00027         $this->MyIsRequired = $IsRequired;
00028         $this->MyLabel = $Label;
00029         $this->MyValidFunc = $ValidFunc;
00030         $this->MyValidMsgs = $ValidMsgs;
00031 
00032         # attempt to set value if available
00033         if (isset($_POST[$this->MyName]))
00034         {
00035             $this->MyValue = $_POST[$this->MyName];
00036         }
00037         elseif (isset($_GET[$this->MyName]))
00038         {
00039             $this->MyValue = $_GET[$this->MyName];
00040         }
00041     }
00046     
00052     function Name($NewVal = NULL) {  return $this->GetOrSet("MyName", $NewVal);  }
00053 
00061     function IsRequired($NewVal = NULL) {  return $this->GetOrSet("MyIsRequired", $NewVal);  }
00062     
00067     function Label($NewVal = NULL) {  return $this->GetOrSet("MyLabel", $NewVal);  }
00068 
00074     function Value($NewVal = NULL) {  return $this->GetOrSet("MyValue", $NewVal);  }
00075     
00080     function IsPassword() {  return method_exists($this, "PasswordFormField");  }
00081     
00086     
00092     function PrintField($DisplayErrorIndicator = FALSE)
00093     {
00094         $this->PrintLabel($DisplayErrorIndicator);
00095         $this->PrintInput($DisplayErrorIndicator);
00096     }
00097 
00103     function PrintLabel($DisplayErrorIndicator = FALSE)
00104     {
00105         # print label
00106         print(($DisplayErrorIndicator ? "<span style=\"color: red;\"" : "")
00107             ."<label for=\"".$this->MyName."\">".$this->MyLabel."</label>"
00108             .($DisplayErrorIndicator ? "</span>" : "")
00109             ."\n");
00110     }
00115     
00120     function IsInvalidValue($Value)
00121     {
00122         # assume value is valid
00123         $ErrorCode = 0;
00124 
00125         # if custom validation function supplied
00126         if ($this->MyValidFunc)
00127         {
00128             # call custom function and return code
00129             $ValidFunc = $this->MyValidFunc;
00130             $ErrorCode = $ValidFunc($this->MyName, $Value);
00131         }
00132         else
00133         {
00134             # if value is required and none is set
00135             if ($this->MyIsRequired && !strlen($Value) 
00136                     && !method_exists($this, "PasswordFormField"))
00137             {
00138                 # return code indicating missing value
00139                 $ErrorCode = 1;
00140             }
00141         }
00142 
00143         # return error code (if any) to caller
00144         return $ErrorCode;
00145     }
00146 
00152     function GetInvalidValueMessage($ErrorCode)
00153     {
00154         $Messages = array(
00155                 0 => "This value is valid.",
00156                 1 => "%L is a required value.",
00157                 );
00158         if (isset($this->MyValidMsgs[$ErrorCode]))
00159         {
00160             $Message = $this->MyValidMsgs[$ErrorCode];
00161         }
00162         else
00163         {
00164             $Message = isset($Messages[$ErrorCode]) 
00165                     ? $Messages[$ErrorCode] : 
00166                             "INTERNAL ERROR - Invalid Error Code (Field = %N, Code = %C)";
00167         }
00168         return $Message;
00169     }
00170 
00173     # ---- PRIVATE INTERFACE -------------------------------------------------
00174 
00175     protected $MyName;
00176     protected $MyIsRequired;
00177     protected $MyLabel;
00178     protected $MyValue;
00179     protected $MyValidFunc;
00180     protected $MyValidMsgs;
00181 
00182     # convenience function to handle getting and setting of values
00183     private function GetOrSet($ValueName, $NewValue)
00184     {
00185         if ($NewValue !== NULL)
00186         {
00187             $this->{$ValueName} = $NewValue;
00188         }
00189         return $this->{$ValueName};
00190     }
00191 }
00192 
00193 
00194 ?>
CWIS logo doxygen
Copyright 2009 Internet Scout