CMS  Version 3.9
validation.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9 Copyright (c) 2007,2008 Sonjara, Inc
10 
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use,
15 copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the
17 Software is furnished to do so, subject to the following
18 conditions:
19 
20 The above copyright notice and this permission notice shall be
21 included in all copies or substantial portions of the Software.
22 
23 Except as contained in this notice, the name(s) of the above
24 copyright holders shall not be used in advertising or otherwise
25 to promote the sale, use or other dealings in this Software
26 without prior written authorization.
27 
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35 OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39  /* Title: validation.inc
40  *
41  * Description: Provides validation for individual questions
42  * in the questionnaire.
43  *
44  * author: Janice Gallant for Sonjara, Inc.
45  *
46  * date: 11/4/09
47  *
48 */
49 
50 Fakoli::usingFeature("validation");
51 
57 class RequiredQuestionValidator extends AbstractValidator
58 {
59  var $empty = "";
60  var $answer; // value of answer object, if view
61 
63  {
64  $this->empty = $empty;
65  $this->answer = $answer;
66  $this->AbstractValidator($field, $title);
67  }
68 
69  function writeClient()
70  {
71  $script = <<<ENDSCRIPT
72 
73  if (form["{$this->field}"].style.display != "none" && form["{$this->field}"].value == "{$this->empty}")
74  {
75  alert("{$this->title} is a required field. Please supply a value.");
76  return true;
77  }
78 ENDSCRIPT;
79 
80  return $script;
81  }
82 
83  function validate()
84  {
85  if(!$this->answer)
86  {
87  global $_POST;
88  $value = "";
89  if($_POST AND (isset($_POST[$this->field]) || $_POST[$this->field] != ""))
90  {
91  $value = $_POST[$this->field];
92  }
93  }
94  else
95  {
96  $value = $this->answer->value;
97  }
98 
99  if ($value != $this->empty)
100  {
101  return "";
102  }
103  else
104  {
105  return "{$this->title} is a required field.";
106  }
107  }
108 }
109 
110 
116 class RequiredCheckListQuestionValidator extends RequiredValidator
117 {
118  var $min;
119  var $answer;
120  var $text;
121 
123  {
124  $this->min = $min;
125  $this->answer = $answer;
126  $this->AbstractValidator($field, $title);
127 
128  $this->text = ($min < 2) ? "checkbox" : "checkboxes";
129  }
130 
131  function writeClient()
132  {
133  $script = <<<ENDSCRIPT
134 
135  var i = 1;
136  var countChecked = 0;
137  var max = form["count_{$this->field}"].value;
138  var fieldName;
139 
140  for(i=1; i <= max; i++)
141  {
142  fieldName = form["{$this->field}["+i+"]"];
143  if(fieldName.checked == true)
144  countChecked += 1;
145  }
146 
147  if ({$this->min} > 0 && countChecked < {$this->min})
148  {
149  alert("You must check at least {$this->min} {$this->text} for {$this->title}.");
150  return false;
151  }
152 ENDSCRIPT;
153 
154  return $script;
155  }
156 
157  function validate()
158  {
159  if(!$this->answer)
160  {
161  global $_POST;
162  $values = $_POST[$this->field];
163  // convert array of checks to comma delimated string and
164  // back again to eliminate blanks
165  if (is_array($values))
166  $valueAnswer = implode(",", array_values($values));
167  }
168  else
169  {
170  $valueAnswer = $this->answer->value;
171  }
172 
173  if (strlen($valueAnswer) > 1)
174  $count = count(explode(",", $valueAnswer));
175  elseif($valueAnswer)
176  $count = 1;
177  else
178  $count = 0;
179 
180  if ($count < $this->min)
181  {
182  return "You must check at least {$this->min} {$this->text} for {$this->title}.";
183  }
184  else
185  {
186  return "";
187  }
188  }
189 }
190 
191 
192 
198 class RequiredRadioButtonQuestionValidator extends RequiredValidator
199 {
200  var $answer;
201 
203  {
204  $this->answer = $answer;
205  $this->AbstractValidator($field, $title);
206  }
207 
208  function writeClient()
209  {
210  $script = <<<ENDSCRIPT
211 
212  var i = 1;
213  var countChecked = 0;
214  var max = form["count_{$this->field}"].value;
215 
216  var fieldName;
217  for(i=1; i <= max; i++)
218  {
219  fieldName = form["{$this->field}_"+i];
220  if(fieldName.checked)
221  countChecked += 1;
222  }
223 
224 
225  if (countChecked < 1)
226  {
227  alert("You must select an option for {$this->title}.");
228  return false;
229  }
230 
231 ENDSCRIPT;
232 
233  return $script;
234  }
235 
236  function validate()
237  {
238  if(!$this->answer)
239  {
240  global $_POST;
241  $value = $_POST[$this->field];
242  }
243  else
244  {
245  $value = $this->answer->value;
246  }
247 
248  if(!$value)
249  {
250  return "You must select an option for {$this->title}.";
251  }
252  else
253  {
254  return "";
255  }
256  }
257 }
258 
270 class RequiredQuestionnaireMessageValidator extends RequiredValidator
271 {
272  var $conditionalFields = array();
273 
274  function __construct($form, $field, $title = "", $message = "")
275  {
276  $this->AbstractValidator($field, $title);
277  $this->conditionalFields[] = "recipients";
278 
279  if($form->data->hasField("additional_recipients"))
280  {
281  $this->conditionalFields[] = "additional_recipients";
282  }
283 
284  if($form->data->hasField("cc_recipients"))
285  {
286  $this->conditionalFields[] = "cc_recipients";
287  }
288  }
289 
290  function writeClient()
291  {
292  if ($this->readOnly) return "";
293 
294  if(count($this->conditionalFields) > 0)
295  {
296  $conditions = array();
297  foreach($this->conditionalFields as $field)
298  {
299  $conditions[] = "form[\"{$field}\"].value != \"{$this->empty}\"";
300  }
301  $conditions = "(" . implode(" || ", $conditions) . ") && ";
302  }
303 
304  $script = <<<ENDSCRIPT
305 
306  if ({$conditions}form["{$this->field}"].value == "{$this->empty}")
307  {
308  alert("{$this->title} is a required field. Please supply a value.");
309  return false;
310  }
311 ENDSCRIPT;
312 
313  return $script;
314  }
315 
316  function validate()
317  {
318  global $_POST;
319 
320  if ($this->readOnly) return "";
321 
322  $empty = true;
323  if(count($this->conditionalFields) > 0)
324  {
325  foreach($this->conditionalFields as $field)
326  {
327  if(isset($_POST[$this->field]) && $_POST[$this->field] !== $this->empty)
328  {
329  $emtpy = false;
330  }
331  }
332  }
333 
334  if ((!isset($_POST[$this->field]) || $_POST[$this->field] === $this->empty)
335  && (!count($this->conditionalFields) || !$empty))
336  {
337  return "{$this->title} is a required field.";
338  }
339  else
340  {
341  return "";
342  }
343  }
344 }
345 ?>
$form
$_POST["owner_id"]
Definition: blog_form.inc:54
$helpTree style
Definition: tree.inc:46
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
RequiredCheckListQuestion Validator.
Definition: validation.inc:117
RequiredCheckListQuestionValidator($field, $title, $min=0, $answer="")
Definition: validation.inc:122
RequiredQuestion Validator.
Definition: validation.inc:58
RequiredQuestionValidator($field, $title, $empty="", $answer="")
Definition: validation.inc:62
If a survey allows anonymous responses, then email message is only required if the user enters a reci...
Definition: validation.inc:271
__construct($form, $field, $title="", $message="")
Definition: validation.inc:274
RequiredRadioButtonQuestion Validator.
Definition: validation.inc:199
RequiredRadioButtonQuestionValidator($field, $title, $answer="")
Definition: validation.inc:202
$message
Definition: mail_to.inc:49
$openData field
$chart max