Projet

Général

Profil

Paste
Télécharger (3,2 ko) Statistiques
| Branche: | Révision:

root / class / FormValidator.class.php @ 9f05628b

1
<?php
2

    
3
class FieldValidationError extends Exception {}
4

    
5
class FormValidator {
6
        /* A tool to validate the parameters of a request (GET/POST) against a set
7
         * of rules.
8
         *
9
         **/
10
        private $errors;
11
        private $sanitized;
12
        public static $validator;
13
  public static $field_validators;
14
  private $format;
15

    
16
        public function __construct($format) {
17
                $this->errors = array();
18
                $this->sanitized = array();
19
    $this->format = $format;
20
        }
21

    
22
        public function validate($request) {
23
                /** Validate the given request
24
                 *  value dict against the validator rules.
25
                 *
26
                 * @param $request a dict like $_REQUEST, $_GET or $_POST
27
                 * @returns true if valid, false else.
28
                 */
29
                $this->errors = array();
30
                $sanitized = array();
31
                foreach($this->format as $fieldname => $validators) {
32
                        $err = false;
33
                        $sanitized_f = false;
34
                        foreach($validators as $validator) {
35
                                if ($validator == 'required') {
36
                                        if (! isset($request[$fieldname])) {
37
                                                $err = 'n\'est pas renseigné';
38
                                                break;
39
                                        } else {
40
                                                $sanitized_f = $request[$fieldname];
41
                                        }
42
                                } else {
43
                                        if (isset($request[$fieldname]) and $request[$fieldname]) {
44
                                                $val = $request[$fieldname];
45
                                                try {
46
                                                        $sanitized_f = $this->validate_field($validator, $val);
47
                                                } catch (FieldValidationError $e) {
48
                                                        $err = $e->getMessage();
49
                                                        break;
50
                                                }
51
                                        }
52
                                }
53
                        }
54
                        if ($err) {
55
                                $this->errors[$fieldname] = $err;
56
                        } else {
57
                                $sanitized[$fieldname] = $sanitized_f;
58
                        }
59
                }
60
                $this->sanitized = $sanitized;
61
                return (count($this->errors) == 0);
62
        }
63

    
64
        public function validate_field($validator, $content) {
65
                /** Returns sanitized value if ok, throws a FieldValidationError otherwise
66
                 */
67
                if (isset(self::$field_validators[$validator])) {
68
                        $f = self::$field_validators[$validator];
69
                        return $f($content);
70
                } else {
71
                        throw new FieldValidationError("'$validator' validator does not exist");
72
                }
73
        }
74

    
75
        public function errors() {
76
                /** An associative array form-key => error
77
                 */
78
                return $this->errors;
79
        }
80

    
81
        public function sane_values() {
82
                return $this->sanitized;
83
        }
84

    
85
        public function print_errors() {
86
                /** raw & quick HTML errors printing, for case that shouldn't happen to users.
87
                 */
88
                echo '<pre>';
89
                var_dump($this->errors());
90
                echo '</pre>';
91
        }
92

    
93
        public static function register($name, $function) {
94
                self::$field_validators[$name] = $function;
95
        }
96
}
97
FormValidator::$field_validators = array();
98

    
99

    
100
FormValidator::register(
101
  'numeric',
102
  function ($v) {
103
          $sanitized = floatval($v);
104
          if (($sanitized === false) || (!is_numeric($v))) {
105
                  throw new FieldValidationError('n\'est pas une valeur numérique');
106
          } else {
107
                  return $sanitized;
108
          }
109
  }
110
);
111

    
112
FormValidator::register(
113
  'positive',
114
  function ($v) {
115
          if ($v < 0) {
116
                  throw new FieldValidationError('est négatif');
117
          } else {
118
                  return floatval($v);
119
          }
120
  }
121
);
122
// Intended to validate checkbox which takes NULL for unchecked
123
FormValidator::register(
124
  'boolean',
125
  function ($v) {
126
          if ($v == NULL) {
127
                  return false;
128
          } else {
129
                  return true;
130
          }
131
  }
132
);
133

    
134
// Validate that it is not a file path
135
FormValidator::register(
136
  'basename',
137
  function ($v) {
138
          if (!strpos($v, '/') && !strpos($v, '\\')) {
139
                  return $v;
140
          } else {
141
                  throw new FieldValidationError('est un chemin');
142
          }
143
  }
144
);
145

    
146

    
147
?>