3 * Copyright (c) STMicroelectronics, 2007. All Rights Reserved.
5 * Originally written by Manuel VACELET, 2007.
6 * Copyright 2019, Franck Villaume - TrivialDev
8 * This file is a part of Fusionforge.
10 * Fusionforge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * Fusionforge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Codendi. If not, see <http://www.gnu.org/licenses/>.
24 require_once 'common/valid/Valid.class.php';
27 * Check that value is a decimal integer greater or equal to zero.
30 class Valid_UInt extends Valid {
31 function validate($value) {
32 $nri = new Rule_Int();
33 $nrgoe = new Rule_GreaterOrEqual(0);
35 $this->addRule($nrgoe);
36 return parent::validate($value);
41 * Check that group_id variable is valid
43 class Valid_GroupId extends Valid {
44 function __construct() {
45 parent::__construct('group_id');
46 //$this->setErrorMessage(_("Error: No group_id was chosen."));
49 function validate($value) {
50 $this->addRule(new Rule_Int());
51 $this->addRule(new Rule_GreaterThan(0));
52 return parent::validate($value);
57 * Check that 'pv' parameter is set to an acceptable value.
59 class Valid_Pv extends Valid {
60 function __construct() {
61 parent::__construct('pv');
64 function validate($value) {
65 $this->addRule(new Rule_WhiteList(array(0,1,2)));
66 return parent::validate($value);
71 * Check that value is a string (should always be true).
73 class Valid_Text extends Valid {
74 function validate($value) {
75 $rs = new Rule_String();
77 return parent::validate($value);
82 * Check that value is a string with neither carrige return nor null char.
84 class Valid_String extends Valid_Text {
85 function validate($value) {
86 $rnc = new Rule_NoCr();
88 return parent::validate($value);
93 * Wrapper for 'WhiteList' rule
95 class Valid_WhiteList extends Valid {
96 function __construct($key, $whitelist) {
97 parent::__construct($key);
98 $this->addRule(new Rule_WhiteList($whitelist));
103 * Check that value match Codendi user short name format.
105 * This rule doesn't check that user actually exists.
107 class Valid_UserNameFormat extends Valid_String {
108 function validate($value) {
109 $this->addRule(new Rule_UserNameFormat());
110 return parent::validate($value);
116 * Check that submitted value is a simple string and a valid Codendi email.
118 class Valid_Email extends Valid_String {
121 function __construct($key=null, $separator=null) {
122 if(is_string($separator)) {
123 $this->separator = $separator;
125 $this->separator = null;
127 parent::__construct($key);
130 function validate($value) {
131 $this->addRule(new Rule_Email($this->separator));
132 return parent::validate($value);
137 * Check uploaded file validity.
139 class Valid_File extends Valid {
142 * Is uploaded file empty or not.
144 * @param array $file One entry of $_FILES
147 function isEmptyValue($file) {
148 if(!is_array($file)) {
150 } elseif (parent::isEmptyValue($file['name'])) {
157 * Check rules on given file.
159 * @param array $files $_FILES superarray.
160 * @param string $index
163 function validate($files, $index='') {
164 if(is_array($files) && isset($files[$index])) {
165 $this->addRule(new Rule_File());
166 return parent::validate($files[$index]);
167 } elseif($this->isRequired) {
177 * If $validator is an instance of a Validator, do nothing and returns it
178 * If $validator is a string and a validator exists (Valid_String for 'string', Valid_UInt for 'uint', ...) then creates an instance and returns it
182 function getInstance($validator, $key = null) {
183 if (is_a($validator, 'Valid')) {
185 } elseif(is_string($validator) && class_exists('Valid_'.$validator)) {
186 $validator_classname = 'Valid_'.$validator;
187 $v = new $validator_classname($key);