| 115 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Project: SmartyValidate: Form Validator for the Smarty Template Engine
|
|
|
5 |
* File: function.validate.php
|
|
|
6 |
* Author: Monte Ohrt <monte at newdigitalgroup dot com>
|
|
|
7 |
*
|
|
|
8 |
* This library is free software; you can redistribute it and/or
|
|
|
9 |
* modify it under the terms of the GNU Lesser General Public
|
|
|
10 |
* License as published by the Free Software Foundation; either
|
|
|
11 |
* version 2.1 of the License, or (at your option) any later version.
|
|
|
12 |
*
|
|
|
13 |
* This library is distributed in the hope that it will be useful,
|
|
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
16 |
* Lesser General Public License for more details.
|
|
|
17 |
*
|
|
|
18 |
* You should have received a copy of the GNU Lesser General Public
|
|
|
19 |
* License along with this library; if not, write to the Free Software
|
|
|
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
21 |
*
|
|
|
22 |
* @link http://www.phpinsider.com/php/code/SmartyValidate/
|
|
|
23 |
* @copyright 2001-2005 New Digital Group, Inc.
|
|
|
24 |
* @author Monte Ohrt <monte at newdigitalgroup dot com>
|
|
|
25 |
* @package SmartyValidate
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
function smarty_function_validate($params, &$smarty) {
|
|
|
29 |
|
|
|
30 |
$_init_params = $smarty->getTemplateVars('validate_init');
|
|
|
31 |
|
|
|
32 |
if(isset($_init_params)) {
|
|
|
33 |
$params = array_merge($_init_params, $params);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
static $_halt = array();
|
|
|
37 |
static $_is_init = null;
|
|
|
38 |
static $_form = null;
|
|
|
39 |
|
|
|
40 |
$_form = SmartyValidate::$form;
|
|
|
41 |
|
|
|
42 |
if(isset($params['form']))
|
|
|
43 |
{
|
|
|
44 |
if($params['form'] != $_form)
|
|
|
45 |
$_is_init = null;
|
|
|
46 |
$_form = $params['form'];
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$_sess =& $_SESSION['SmartyValidate'][$_form];
|
|
|
50 |
|
|
|
51 |
if(!isset($_is_init))
|
|
|
52 |
$_is_init = $_sess['is_init'];
|
|
|
53 |
|
|
|
54 |
if(!SmartyValidate::is_registered_form($_form)) {
|
|
|
55 |
trigger_error("SmartyValidate: [validate plugin] form '$_form' is not registered.");
|
|
|
56 |
return false;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
if(isset($_halt[$_form]) && $_halt[$_form])
|
|
|
60 |
return;
|
|
|
61 |
|
|
|
62 |
if (!class_exists('SmartyValidate')) {
|
|
|
63 |
$smarty->trigger_error("validate: missing SmartyValidate class");
|
|
|
64 |
return;
|
|
|
65 |
}
|
|
|
66 |
if (!isset($_SESSION['SmartyValidate'])) {
|
|
|
67 |
$smarty->trigger_error("validate: SmartyValidate is not initialized, use connect() first");
|
|
|
68 |
return;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
if(isset($params['id'])) {
|
|
|
72 |
if (($_validator_key = SmartyValidate::is_registered_validator($params['id'], $_form)) === false) {
|
|
|
73 |
$smarty->trigger_error("validate: validator id '" . $params['id'] . "' is not registered.");
|
|
|
74 |
return;
|
|
|
75 |
}
|
|
|
76 |
} else {
|
|
|
77 |
if (strlen($params['field']) == 0) {
|
|
|
78 |
$smarty->trigger_error("validate: missing 'field' parameter");
|
|
|
79 |
return;
|
|
|
80 |
}
|
|
|
81 |
if (strlen($params['criteria']) == 0) {
|
|
|
82 |
$smarty->trigger_error("validate: missing 'criteria' parameter");
|
|
|
83 |
return;
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
if(isset($params['trim'])) {
|
|
|
87 |
$params['trim'] = SmartyValidate::_booleanize($params['trim']);
|
|
|
88 |
}
|
|
|
89 |
if(isset($params['empty'])) {
|
|
|
90 |
$params['empty'] = SmartyValidate::_booleanize($params['empty']);
|
|
|
91 |
}
|
|
|
92 |
if(isset($params['halt'])) {
|
|
|
93 |
$params['halt'] = SmartyValidate::_booleanize($params['halt']);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
if(isset($_sess['validators']) && is_array($_sess['validators'])) {
|
|
|
97 |
if(isset($params['id'])) {
|
|
|
98 |
if($_is_init) {
|
|
|
99 |
$_sess['validators'][$_validator_key]['message'] = $params['message'];
|
|
|
100 |
}
|
|
|
101 |
} else {
|
|
|
102 |
foreach($_sess['validators'] as $_key => $_field) {
|
|
|
103 |
if($_field['field'] == $params['field']
|
|
|
104 |
&& $_field['criteria'] == $params['criteria']) {
|
|
|
105 |
// field exists
|
|
|
106 |
$_validator_key = $_key;
|
|
|
107 |
break;
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if(!$_is_init) {
|
|
|
113 |
|
|
|
114 |
if(!$_sess['is_error']) // no validation error
|
|
|
115 |
return;
|
|
|
116 |
|
|
|
117 |
if(!isset($_sess['validators'][$_validator_key]['valid']) || !$_sess['validators'][$_validator_key]['valid']) {
|
|
|
118 |
// not valid, show error and reset
|
|
|
119 |
$_halt[$_form] = isset($_sess['validators'][$_validator_key]['halt'])
|
|
|
120 |
? $_sess['validators'][$_validator_key]['halt']
|
|
|
121 |
: false;
|
|
|
122 |
$_echo = true;
|
|
|
123 |
if(isset($params['assign'])) {
|
|
|
124 |
$smarty->assign($params['assign'], $_sess['validators'][$_validator_key]['message']);
|
|
|
125 |
} elseif (isset($params['append'])) {
|
|
|
126 |
$smarty->append($params['append'], $_sess['validators'][$_validator_key]['message']);
|
|
|
127 |
} else {
|
|
|
128 |
// no assign or append, so echo message
|
|
|
129 |
echo $_sess['validators'][$_validator_key]['message'];
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
} else {
|
|
|
133 |
if(isset($params['id'])) {
|
|
|
134 |
$_sess['validators'][$_validator_key] =
|
|
|
135 |
array_merge($_sess['validators'][$_validator_key], $params);
|
|
|
136 |
} else {
|
|
|
137 |
$_params = $params;
|
|
|
138 |
$_params['valid'] = false;
|
|
|
139 |
$_sess['validators'][] = $_params;
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
$_sess['is_init'] = false;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
?>
|