CWIS Developer Documentation
FormField.php
Go to the documentation of this file.
1 <?PHP
2 
6 class FormField {
7 
8  # ---- PUBLIC INTERFACE --------------------------------------------------
9 
12 
23  function FormField($Name, $IsRequired, $Label, $ValidFunc, $ValidMsgs)
24  {
25  # save field info
26  $this->MyName = $Name;
27  $this->MyIsRequired = $IsRequired;
28  $this->MyLabel = $Label;
29  $this->MyValidFunc = $ValidFunc;
30  $this->MyValidMsgs = $ValidMsgs;
31 
32  # attempt to set value if available
33  if (isset($_POST[$this->MyName]))
34  {
35  $this->MyValue = $_POST[$this->MyName];
36  }
37  elseif (isset($_GET[$this->MyName]))
38  {
39  $this->MyValue = $_GET[$this->MyName];
40  }
41  }
46 
52  function Name($NewVal = NULL) { return $this->GetOrSet("MyName", $NewVal); }
53 
61  function IsRequired($NewVal = NULL) { return $this->GetOrSet("MyIsRequired", $NewVal); }
62 
67  function Label($NewVal = NULL) { return $this->GetOrSet("MyLabel", $NewVal); }
68 
74  function Value($NewVal = NULL) { return $this->GetOrSet("MyValue", $NewVal); }
75 
80  function IsPassword() { return method_exists($this, "PasswordFormField"); }
81 
86 
92  function PrintField($DisplayErrorIndicator = FALSE)
93  {
94  $this->PrintLabel($DisplayErrorIndicator);
95  $this->PrintInput($DisplayErrorIndicator);
96  }
97 
103  function PrintLabel($DisplayErrorIndicator = FALSE)
104  {
105  # print label
106  print(($DisplayErrorIndicator ? "<span style=\"color: red;\"" : "")
107  ."<label for=\"".$this->MyName."\">".$this->MyLabel."</label>"
108  .($DisplayErrorIndicator ? "</span>" : "")
109  ."\n");
110  }
115 
120  function IsInvalidValue($Value)
121  {
122  # assume value is valid
123  $ErrorCode = 0;
124 
125  # if custom validation function supplied
126  if ($this->MyValidFunc)
127  {
128  # call custom function and return code
129  $ValidFunc = $this->MyValidFunc;
130  $ErrorCode = $ValidFunc($this->MyName, $Value);
131  }
132  else
133  {
134  # if value is required and none is set
135  if ($this->MyIsRequired && !strlen($Value)
136  && !method_exists($this, "PasswordFormField"))
137  {
138  # return code indicating missing value
139  $ErrorCode = 1;
140  }
141  }
142 
143  # return error code (if any) to caller
144  return $ErrorCode;
145  }
146 
152  function GetInvalidValueMessage($ErrorCode)
153  {
154  $Messages = array(
155  0 => "This value is valid.",
156  1 => "%L is a required value.",
157  );
158  if (isset($this->MyValidMsgs[$ErrorCode]))
159  {
160  $Message = $this->MyValidMsgs[$ErrorCode];
161  }
162  else
163  {
164  $Message = isset($Messages[$ErrorCode])
165  ? $Messages[$ErrorCode] :
166  "INTERNAL ERROR - Invalid Error Code (Field = %N, Code = %C)";
167  }
168  return $Message;
169  }
170 
173  # ---- PRIVATE INTERFACE -------------------------------------------------
174 
175  protected $MyName;
176  protected $MyIsRequired;
177  protected $MyLabel;
178  protected $MyValue;
179  protected $MyValidFunc;
180  protected $MyValidMsgs;
181 
182  # convenience function to handle getting and setting of values
183  private function GetOrSet($ValueName, $NewValue)
184  {
185  if ($NewValue !== NULL)
186  {
187  $this->{$ValueName} = $NewValue;
188  }
189  return $this->{$ValueName};
190  }
191 }
192 
193 
194 ?>