3 * Copyright (c) STMicroelectronics, 2007. All Rights Reserved.
5 * Originally written by Manuel VACELET, 2007.
7 * This file is a part of Codendi.
9 * Codendi is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Codendi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Codendi. If not, see <http://www.gnu.org/licenses/>.
23 require_once 'common/valid/Rule.class.php';
57 var $globalErrorMessage;
67 function Valid($key = null) {
69 $this->errors = array();
70 $this->rules = array();
71 $this->isRequired = false;
72 $this->useFeedback = true;
73 $this->globalErrorMessage = null;
78 * Return the variable name on which rules must applies.
87 * Add a new rule in this validation.
89 * ou can add a custom error message that will bypass the default one that
90 * comes with the rule.
91 * @param Rule Reference on rule.
92 * @param String Error message.
94 function addRule(&$rule, $message=false) {
95 $this->rules[] =& $rule;
96 $this->errors[] = $message;
100 * The value is required.
102 * All rules must succeed (as usual). Empty / null values are forbidden
103 * (raise an error). And all failure generate an error (instead of a
106 function required() {
107 $this->isRequired = true;
113 function disableFeedback() {
114 $this->useFeedback = false;
118 * Set a global error message that will replace all other messages.
120 * Note: If no error, no message raised. The message is raised with either
121 * 'warning' or 'error' level according to required();
122 * @param String Error message
124 function setErrorMessage($msg) {
125 $this->globalErrorMessage = $msg;
129 * Return true if given value is empty
132 * @param mixed Value to test
135 function isValueEmpty($value) {
136 return ($value === '' || $value === false || $value === null);
140 * Append feebback in the global Response object.
143 function addFeedback($level, $error) {
144 $GLOBALS['feedback'].= $level.": ". $error;
148 * Generate error message according to settings.
150 * Takes in account user requirement 'required' and
151 * 'disableFeedback'. Empty error messages are disarded.
154 function populateFeedback() {
155 if($this->useFeedback) {
157 if($this->isRequired) {
160 if($this->globalErrorMessage !== null &&
162 $this->addFeedback($level, $this->globalErrorMessage);
164 foreach($this->errors as $error) {
166 $this->addFeedback($level, $error);
174 * Prepare error message on Rule:isValid result.
176 * If the test succeeded, the error message is cleared (either custom or
177 * built-in messages).
179 * @param Integer Index of the Rule that was applied.
180 * @param Boolean Result of the test.
182 function errorMessage($i, $result) {
183 if($result === true) {
184 $this->errors[$i] = '';
186 if($this->errors[$i] === false) {
187 $this->errors[$i] = $this->rules[$i]->getErrorMessage($this->key);
193 * Apply each rule on the given value and prepare feedback.
196 * @param mixed Value to test.
198 function checkEachRules($value) {
200 $rCtr = count($this->rules);
201 for($i = 0; $i < $rCtr; $i++) {
202 $valid = $this->rules[$i]->isValid($value);
203 $this->errorMessage($i, $valid);
204 $isValid = $isValid && $valid;
206 if($isValid && $this->isRequired && $this->isValueEmpty($value)) {
207 $this->isValid = false;
209 $this->isValid = $isValid;
211 $this->populateFeedback();
215 * Run validation on given value.
217 * @param mixed Value to test.
219 function validate($value) {
221 || (!$this->isRequired && !$this->isValueEmpty($value))) {
222 $this->checkEachRules($value);
223 return $this->isValid;