Projet

Général

Profil

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

root / class / FormValidator.class.php @ c621f296

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

    
62
                return ($err == false);
63
        }
64

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

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

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

    
86
        public static function register($name, $function) {
87
                self::$field_validators[$name] = $function;
88
        }
89
}
90
FormValidator::$field_validators = array();
91

    
92

    
93
FormValidator::register(
94
  'numeric',
95
  function ($v) {
96
          $sanitized = floatval($v);
97
          if (($sanitized === false) || (!is_numeric($v))) {
98
                  throw new FieldValidationError('n\'est pas une valeur numérique');
99
          } else {
100
                  return $sanitized;
101
          }
102
  }
103
);
104

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

    
127
?>