CWIS Developer Documentation
FormField.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: FormField.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2002-2015 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
14 class FormField
15 {
16  # ---- PUBLIC INTERFACE --------------------------------------------------
17 
20 
32  public function __construct($Name, $IsRequired, $Label, $ValidFunc, $ValidMsgs)
33  {
34  # save field info
35  $this->MyName = $Name;
36  $this->MyIsRequired = $IsRequired;
37  $this->MyLabel = $Label;
38  $this->MyValidFunc = $ValidFunc;
39  $this->MyValidMsgs = $ValidMsgs;
40 
41  # attempt to set value if available
42  if (isset($_POST[$this->MyName]))
43  {
44  $this->MyValue = $_POST[$this->MyName];
45  }
46  elseif (isset($_GET[$this->MyName]))
47  {
48  $this->MyValue = $_GET[$this->MyName];
49  }
50  }
55 
63  public function Name($NewVal = NULL)
64  {
65  return $this->GetOrSet("MyName", $NewVal);
66  }
67 
76  public function IsRequired($NewVal = NULL)
77  {
78  return $this->GetOrSet("MyIsRequired", $NewVal);
79  }
80 
86  public function Label($NewVal = NULL)
87  {
88  return $this->GetOrSet("MyLabel", $NewVal);
89  }
90 
97  public function Value($NewVal = NULL)
98  {
99  return $this->GetOrSet("MyValue", $NewVal);
100  }
101 
106  public function IsPassword()
107  {
108  return method_exists($this, "PasswordFormField");
109  }
110 
115 
122  public function PrintField($DisplayErrorIndicator = FALSE)
123  {
124  $this->PrintLabel($DisplayErrorIndicator);
125  $this->PrintInput($DisplayErrorIndicator);
126  }
127 
134  public function PrintLabel($DisplayErrorIndicator = FALSE)
135  {
136  # print label
137  print(($DisplayErrorIndicator ? "<span style=\"color: red;\">" : "")
138  ."<label for=\"".$this->MyName."\">".$this->MyLabel."</label>"
139  .($DisplayErrorIndicator ? "</span>" : "")
140  ."\n");
141  }
146 
152  public function IsInvalidValue($Value)
153  {
154  # assume value is valid
155  $ErrorCode = 0;
156 
157  # if custom validation function supplied
158  if ($this->MyValidFunc)
159  {
160  # call custom function and return code
161  $ValidFunc = $this->MyValidFunc;
162  $ErrorCode = $ValidFunc($this->MyName, $Value);
163  }
164  else
165  {
166  # if value is required and none is set
167  if ($this->MyIsRequired && !strlen($Value)
168  && !method_exists($this, "PasswordFormField"))
169  {
170  # return code indicating missing value
171  $ErrorCode = 1;
172  }
173  }
174 
175  # return error code (if any) to caller
176  return $ErrorCode;
177  }
178 
184  public function GetInvalidValueMessage($ErrorCode)
185  {
186  $Messages = array(
187  0 => "This value is valid.",
188  1 => "%L is a required value.",
189  );
190  if (isset($this->MyValidMsgs[$ErrorCode]))
191  {
192  $Message = $this->MyValidMsgs[$ErrorCode];
193  }
194  else
195  {
196  $Message = isset($Messages[$ErrorCode])
197  ? $Messages[$ErrorCode] :
198  "INTERNAL ERROR - Invalid Error Code (Field = %N, Code = %C)";
199  }
200  return $Message;
201  }
202 
205  # ---- PRIVATE INTERFACE -------------------------------------------------
206 
207  protected $MyName;
208  protected $MyIsRequired;
209  protected $MyLabel;
210  protected $MyValue;
211  protected $MyValidFunc;
212  protected $MyValidMsgs;
213 
221  private function GetOrSet($ValueName, $NewValue)
222  {
223  if ($NewValue !== NULL)
224  {
225  $this->{$ValueName} = $NewValue;
226  }
227  return $this->{$ValueName};
228  }
229 }
PrintField($DisplayErrorIndicator=FALSE)
Print the form field label and generate input tags for this form field.
Definition: FormField.php:122
IsInvalidValue($Value)
Check the validity of the form field&#39;s value.
Definition: FormField.php:152
IsRequired($NewVal=NULL)
Get/set IsRequired.
Definition: FormField.php:76
__construct($Name, $IsRequired, $Label, $ValidFunc, $ValidMsgs)
Object Constructor.
Definition: FormField.php:32
Generator for HTML form fields.
Definition: FormField.php:14
GetInvalidValueMessage($ErrorCode)
Map an error code from IsInvalidValue to an error message.
Definition: FormField.php:184
IsPassword()
Determine if this form field is a password form field.
Definition: FormField.php:106
Label($NewVal=NULL)
Get or set the form field label as shown to the user.
Definition: FormField.php:86
Name($NewVal=NULL)
Get or set the name.
Definition: FormField.php:63
PrintLabel($DisplayErrorIndicator=FALSE)
Print the label for this form field without generating the input tags.
Definition: FormField.php:134
Value($NewVal=NULL)
Get or set the form field value.
Definition: FormField.php:97