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-2013 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  function FormField($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  function Name($NewVal = NULL) { return $this->GetOrSet("MyName", $NewVal); }
64 
73  function IsRequired($NewVal = NULL) { return $this->GetOrSet("MyIsRequired", $NewVal); }
74 
80  function Label($NewVal = NULL) { return $this->GetOrSet("MyLabel", $NewVal); }
81 
88  function Value($NewVal = NULL) { return $this->GetOrSet("MyValue", $NewVal); }
89 
94  function IsPassword() { return method_exists($this, "PasswordFormField"); }
95 
100 
107  function PrintField($DisplayErrorIndicator = FALSE)
108  {
109  $this->PrintLabel($DisplayErrorIndicator);
110  $this->PrintInput($DisplayErrorIndicator);
111  }
112 
119  function PrintLabel($DisplayErrorIndicator = FALSE)
120  {
121  # print label
122  print(($DisplayErrorIndicator ? "<span style=\"color: red;\">" : "")
123  ."<label for=\"".$this->MyName."\">".$this->MyLabel."</label>"
124  .($DisplayErrorIndicator ? "</span>" : "")
125  ."\n");
126  }
131 
136  function IsInvalidValue($Value)
137  {
138  # assume value is valid
139  $ErrorCode = 0;
140 
141  # if custom validation function supplied
142  if ($this->MyValidFunc)
143  {
144  # call custom function and return code
145  $ValidFunc = $this->MyValidFunc;
146  $ErrorCode = $ValidFunc($this->MyName, $Value);
147  }
148  else
149  {
150  # if value is required and none is set
151  if ($this->MyIsRequired && !strlen($Value)
152  && !method_exists($this, "PasswordFormField"))
153  {
154  # return code indicating missing value
155  $ErrorCode = 1;
156  }
157  }
158 
159  # return error code (if any) to caller
160  return $ErrorCode;
161  }
162 
168  function GetInvalidValueMessage($ErrorCode)
169  {
170  $Messages = array(
171  0 => "This value is valid.",
172  1 => "%L is a required value.",
173  );
174  if (isset($this->MyValidMsgs[$ErrorCode]))
175  {
176  $Message = $this->MyValidMsgs[$ErrorCode];
177  }
178  else
179  {
180  $Message = isset($Messages[$ErrorCode])
181  ? $Messages[$ErrorCode] :
182  "INTERNAL ERROR - Invalid Error Code (Field = %N, Code = %C)";
183  }
184  return $Message;
185  }
186 
189  # ---- PRIVATE INTERFACE -------------------------------------------------
190 
191  protected $MyName;
192  protected $MyIsRequired;
193  protected $MyLabel;
194  protected $MyValue;
195  protected $MyValidFunc;
196  protected $MyValidMsgs;
197 
205  private function GetOrSet($ValueName, $NewValue)
206  {
207  if ($NewValue !== NULL)
208  {
209  $this->{$ValueName} = $NewValue;
210  }
211  return $this->{$ValueName};
212  }
213 
214 }
PrintField($DisplayErrorIndicator=FALSE)
Print the form field label and generate input tags for this form field.
Definition: FormField.php:107
IsInvalidValue($Value)
Check the validity of the form field's value.
Definition: FormField.php:136
IsRequired($NewVal=NULL)
Get/set IsRequired.
Definition: FormField.php:73
Generator for HTML form fields.
Definition: FormField.php:14
GetInvalidValueMessage($ErrorCode)
Map an error code from IsInvalidValue to an error message.
Definition: FormField.php:168
FormField($Name, $IsRequired, $Label, $ValidFunc, $ValidMsgs)
Object Constructor.
Definition: FormField.php:32
PHP
Definition: OAIClient.php:39
IsPassword()
Determine if this form field is a password form field.
Definition: FormField.php:94
Label($NewVal=NULL)
Get or set the form field label as shown to the user.
Definition: FormField.php:80
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:119
Value($NewVal=NULL)
Get or set the form field value.
Definition: FormField.php:88