├── LICENSE ├── Validation.php └── readme.md /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Davide Cesarano 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Validation.php: -------------------------------------------------------------------------------- 1 | 9 | * @copyright (c) 2016, Davide Cesarano 10 | * @license https://github.com/davidecesarano/Validation/blob/master/LICENSE MIT License 11 | * @link https://github.com/davidecesarano/Validation 12 | */ 13 | 14 | class Validation { 15 | 16 | /** 17 | * @var array $patterns 18 | */ 19 | public $patterns = array( 20 | 'uri' => '[A-Za-z0-9-\/_?&=]+', 21 | 'url' => '[A-Za-z0-9-:.\/_?&=#]+', 22 | 'alpha' => '[\p{L}]+', 23 | 'words' => '[\p{L}\s]+', 24 | 'alphanum' => '[\p{L}0-9]+', 25 | 'int' => '[0-9]+', 26 | 'float' => '[0-9\.,]+', 27 | 'tel' => '[0-9+\s()-]+', 28 | 'text' => '[\p{L}0-9\s-.,;:!"%&()?+\'°#\/@]+', 29 | 'file' => '[\p{L}\s0-9-_!%&()=\[\]#@,.;+]+\.[A-Za-z0-9]{2,4}', 30 | 'folder' => '[\p{L}\s0-9-_!%&()=\[\]#@,.;+]+', 31 | 'address' => '[\p{L}0-9\s.,()°-]+', 32 | 'date_dmy' => '[0-9]{1,2}\-[0-9]{1,2}\-[0-9]{4}', 33 | 'date_ymd' => '[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}', 34 | 'email' => '[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+[.]+[a-z-A-Z]' 35 | ); 36 | 37 | /** 38 | * @var array $errors 39 | */ 40 | public $errors = array(); 41 | 42 | /** 43 | * Nome del campo 44 | * 45 | * @param string $name 46 | * @return this 47 | */ 48 | public function name($name){ 49 | 50 | $this->name = $name; 51 | return $this; 52 | 53 | } 54 | 55 | /** 56 | * Valore del campo 57 | * 58 | * @param mixed $value 59 | * @return this 60 | */ 61 | public function value($value){ 62 | 63 | $this->value = $value; 64 | return $this; 65 | 66 | } 67 | 68 | /** 69 | * File 70 | * 71 | * @param mixed $value 72 | * @return this 73 | */ 74 | public function file($value){ 75 | 76 | $this->file = $value; 77 | return $this; 78 | 79 | } 80 | 81 | /** 82 | * Pattern da applicare al riconoscimento 83 | * dell'espressione regolare 84 | * 85 | * @param string $name nome del pattern 86 | * @return this 87 | */ 88 | public function pattern($name){ 89 | 90 | if($name == 'array'){ 91 | 92 | if(!is_array($this->value)){ 93 | $this->errors[] = 'Formato campo '.$this->name.' non valido.'; 94 | } 95 | 96 | }else{ 97 | 98 | $regex = '/^('.$this->patterns[$name].')$/u'; 99 | if($this->value != '' && !preg_match($regex, $this->value)){ 100 | $this->errors[] = 'Formato campo '.$this->name.' non valido.'; 101 | } 102 | 103 | } 104 | return $this; 105 | 106 | } 107 | 108 | /** 109 | * Pattern personalizzata 110 | * 111 | * @param string $pattern 112 | * @return this 113 | */ 114 | public function customPattern($pattern){ 115 | 116 | $regex = '/^('.$pattern.')$/u'; 117 | if($this->value != '' && !preg_match($regex, $this->value)){ 118 | $this->errors[] = 'Formato campo '.$this->name.' non valido.'; 119 | } 120 | return $this; 121 | 122 | } 123 | 124 | /** 125 | * Campo obbligatorio 126 | * 127 | * @return this 128 | */ 129 | public function required(){ 130 | 131 | if((isset($this->file) && $this->file['error'] == 4) || ($this->value == '' || $this->value == null)){ 132 | $this->errors[] = 'Campo '.$this->name.' obbligatorio.'; 133 | } 134 | return $this; 135 | 136 | } 137 | 138 | /** 139 | * Lunghezza minima 140 | * del valore del campo 141 | * 142 | * @param int $min 143 | * @return this 144 | */ 145 | public function min($length){ 146 | 147 | if(is_string($this->value)){ 148 | 149 | if(strlen($this->value) < $length){ 150 | $this->errors[] = 'Valore campo '.$this->name.' inferiore al valore minimo'; 151 | } 152 | 153 | }else{ 154 | 155 | if($this->value < $length){ 156 | $this->errors[] = 'Valore campo '.$this->name.' inferiore al valore minimo'; 157 | } 158 | 159 | } 160 | return $this; 161 | 162 | } 163 | 164 | /** 165 | * Lunghezza massima 166 | * del valore del campo 167 | * 168 | * @param int $max 169 | * @return this 170 | */ 171 | public function max($length){ 172 | 173 | if(is_string($this->value)){ 174 | 175 | if(strlen($this->value) > $length){ 176 | $this->errors[] = 'Valore campo '.$this->name.' superiore al valore massimo'; 177 | } 178 | 179 | }else{ 180 | 181 | if($this->value > $length){ 182 | $this->errors[] = 'Valore campo '.$this->name.' superiore al valore massimo'; 183 | } 184 | 185 | } 186 | return $this; 187 | 188 | } 189 | 190 | /** 191 | * Confronta con il valore di 192 | * un altro campo 193 | * 194 | * @param mixed $value 195 | * @return this 196 | */ 197 | public function equal($value){ 198 | 199 | if($this->value != $value){ 200 | $this->errors[] = 'Valore campo '.$this->name.' non corrispondente.'; 201 | } 202 | return $this; 203 | 204 | } 205 | 206 | /** 207 | * Dimensione massima del file 208 | * 209 | * @param int $size 210 | * @return this 211 | */ 212 | public function maxSize($size){ 213 | 214 | if($this->file['error'] != 4 && $this->file['size'] > $size){ 215 | $this->errors[] = 'Il file '.$this->name.' supera la dimensione massima di '.number_format($size / 1048576, 2).' MB.'; 216 | } 217 | return $this; 218 | 219 | } 220 | 221 | /** 222 | * Estensione (formato) del file 223 | * 224 | * @param string $extension 225 | * @return this 226 | */ 227 | public function ext($extension){ 228 | 229 | if($this->file['error'] != 4 && pathinfo($this->file['name'], PATHINFO_EXTENSION) != $extension && strtoupper(pathinfo($this->file['name'], PATHINFO_EXTENSION)) != $extension){ 230 | $this->errors[] = 'Il file '.$this->name.' non è un '.$extension.'.'; 231 | } 232 | return $this; 233 | 234 | } 235 | 236 | /** 237 | * Purifica per prevenire attacchi XSS 238 | * 239 | * @param string $string 240 | * @return $string 241 | */ 242 | public function purify($string){ 243 | return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); 244 | } 245 | 246 | /** 247 | * Campi validati 248 | * 249 | * @return boolean 250 | */ 251 | public function isSuccess(){ 252 | if(empty($this->errors)) return true; 253 | } 254 | 255 | /** 256 | * Errori della validazione 257 | * 258 | * @return array $this->errors 259 | */ 260 | public function getErrors(){ 261 | if(!$this->isSuccess()) return $this->errors; 262 | } 263 | 264 | /** 265 | * Visualizza errori in formato Html 266 | * 267 | * @return string $html 268 | */ 269 | public function displayErrors(){ 270 | 271 | $html = ''; 276 | 277 | return $html; 278 | 279 | } 280 | 281 | /** 282 | * Visualizza risultato della validazione 283 | * 284 | * @return booelan|string 285 | */ 286 | public function result(){ 287 | 288 | if(!$this->isSuccess()){ 289 | 290 | foreach($this->getErrors() as $error){ 291 | echo "$error\n"; 292 | } 293 | exit; 294 | 295 | }else{ 296 | return true; 297 | } 298 | 299 | } 300 | 301 | /** 302 | * Verifica se il valore è 303 | * un numero intero 304 | * 305 | * @param mixed $value 306 | * @return boolean 307 | */ 308 | public static function is_int($value){ 309 | if(filter_var($value, FILTER_VALIDATE_INT)) return true; 310 | } 311 | 312 | /** 313 | * Verifica se il valore è 314 | * un numero float 315 | * 316 | * @param mixed $value 317 | * @return boolean 318 | */ 319 | public static function is_float($value){ 320 | if(filter_var($value, FILTER_VALIDATE_FLOAT)) return true; 321 | } 322 | 323 | /** 324 | * Verifica se il valore è 325 | * una lettera dell'alfabeto 326 | * 327 | * @param mixed $value 328 | * @return boolean 329 | */ 330 | public static function is_alpha($value){ 331 | if(filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z]+$/")))) return true; 332 | } 333 | 334 | /** 335 | * Verifica se il valore è 336 | * una lettera o un numero 337 | * 338 | * @param mixed $value 339 | * @return boolean 340 | */ 341 | public static function is_alphanum($value){ 342 | if(filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[a-zA-Z0-9]+$/")))) return true; 343 | } 344 | 345 | /** 346 | * Verifica se il valore è 347 | * un url 348 | * 349 | * @param mixed $value 350 | * @return boolean 351 | */ 352 | public static function is_url($value){ 353 | if(filter_var($value, FILTER_VALIDATE_URL)) return true; 354 | } 355 | 356 | /** 357 | * Verifica se il valore è 358 | * un uri 359 | * 360 | * @param mixed $value 361 | * @return boolean 362 | */ 363 | public static function is_uri($value){ 364 | if(filter_var($value, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "/^[A-Za-z0-9-\/_]+$/")))) return true; 365 | } 366 | 367 | /** 368 | * Verifica se il valore è 369 | * true o false 370 | * 371 | * @param mixed $value 372 | * @return boolean 373 | */ 374 | public static function is_bool($value){ 375 | if(is_bool(filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))) return true; 376 | } 377 | 378 | /** 379 | * Verifica se il valore è 380 | * un'e-mail 381 | * 382 | * @param mixed $value 383 | * @return boolean 384 | */ 385 | public static function is_email($value){ 386 | if(filter_var($value, FILTER_VALIDATE_EMAIL)) return true; 387 | } 388 | 389 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Simple PHP class for Validation 2 | 3 | This PHP class is useful to validate an HTML form fields. 4 | 5 | > :warning: Do you use PSR-7? Try [Embryo Validation](https://github.com/davidecesarano/Embryo-Validation), the new version of this repository. 6 | 7 | ## Usage 8 | ```php 9 | require_once('Validation.php'); 10 | ``` 11 | 12 | ## Typical Use 13 | ```php 14 | $email = 'example@email.com'; 15 | $username = 'admin'; 16 | $password = 'test'; 17 | $age = 29; 18 | 19 | $val = new Validation(); 20 | $val->name('email')->value($email)->pattern('email')->required(); 21 | $val->name('username')->value($username)->pattern('alpha')->required(); 22 | $val->name('password')->value($password)->customPattern('[A-Za-z0-9-.;_!#@]{5,15}')->required(); 23 | $val->name('age')->value($age)->min(18)->max(40); 24 | 25 | if($val->isSuccess()){ 26 | echo "Validation ok!"; 27 | }else{ 28 | echo "Validation error!"; 29 | var_dump($val->getErrors()); 30 | } 31 | ``` 32 | 33 | ## Simple Form HTML Use 34 | ```php 35 | 36 | 37 |
38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
48 | 49 | name('name')->value($_POST['name'])->pattern('words')->required(); 54 | $val->name('e-mail')->value($_POST['email'])->pattern('email')->required(); 55 | $val->name('tel')->value($_POST['tel'])->pattern('tel'); 56 | $val->name('message')->value($_POST['message'])->pattern('text')->required(); 57 | 58 | if($val->isSuccess()){ 59 | echo 'Validation ok!'; 60 | }else{ 61 | echo $val->displayErrors(); 62 | } 63 | 64 | } 65 | 66 | ?> 67 | ``` 68 | 69 | ## Methods 70 | 71 | | Method | Parameter | Description | Example | 72 | |-----------------|-----------|-----------------------------------------------------------------------------|-----------------------------------| 73 | | name | $name | Return field name | name('name') | 74 | | value | $value | Return value field | value($_POST['name]) | 75 | | file | $value | Return $_FILES array | file($_FILES['name']) | 76 | | pattern | $pattern | Return an error if the input has a different format than the pattern | pattern('text') | 77 | | customPattern | $pattern | Return an error if the input has a different format than the custom pattern | customPattern('[A-Za-z]') | 78 | | required | | Returns an error if the input is empty | required() | 79 | | min | $length | Return an error if the input is shorter than the parameter | min(10) | 80 | | max | $length | Return an error if the input is longer than the parameter | max(10) | 81 | | equal | $value | Return an error if the input is not same as the parameter | equal($value) | 82 | | maxSize | $value | Return an error if the file size exceeds the maximum allowable size | maxSize(3145728) | 83 | | ext | $value | Return an error if the file extension is not same the parameter | ext('pdf') | 84 | | isSuccess | | Return true if there are no errors | isSuccess() | 85 | | getErrors | | Return an array with validation errors | getErrors() | 86 | | displayErrors | | Return Html errors | displayErrors() | 87 | | result | | Return true if there are no errors or html errors | result() | 88 | | is_int | $value | Return true if the value is an integer number | is_int(1) | 89 | | is_float | $value | Return true if the value is an float number | is_float(1.1) | 90 | | is_alpha | $value | Return true if the value is an alphabetic characters | is_alpha('test') | 91 | | is_alphanum | $value | Return true if the value is an alphanumeric characters | is_alphanum('test1') | 92 | | is_url | $value | Return true if the value is an url (protocol is required) | is_url('http://www.example.com') | 93 | | is_uri | $value | Return true if the value is an uri (protocol is not required) | is_uri('www.example.com') | 94 | | is_bool | $value | Return true if the value is an boolean | is_bool(true) | 95 | | is_email | $value | Return true if the value is an e-mail | is_email('email@email.com') | 96 | 97 | ## Patterns 98 | 99 | | Name | Description | Example | 100 | |----------|--------------------------------------------------------------------|-----------------------------------| 101 | | uri | Url without file extension | folder-1/folder-2 | 102 | | url | Uri with file extension | http://www.example.com/myfile.gif | 103 | | alpha | Only alphabetic characters | World | 104 | | words | Alphabetic characters and spaces | Hello World | 105 | | alphanum | Alpha-numeric characters | test2016 | 106 | | int | Integer number | 154 | 107 | | float | Float number | 1,234.56 | 108 | | tel | Telephone number | (+39) 081-777-77-77 | 109 | | text | Alpha-numeric characters, spaces and some special characters | Test1 ,.():;!@&%? | 110 | | file | File name format | myfile.png | 111 | | folder | Folder name format | my_folde | 112 | | address | Address format | Street Name, 99 | 113 | | date_dmy | Date in format dd-MM-YYYY | 01-01-2016 | 114 | | date_ymd | Date in format YYYY-MM-dd | 2016-01-01 | 115 | | email | E-Mail format | exampe@email.com | 116 | --------------------------------------------------------------------------------