forked from webloper/PHP-Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
93 lines (83 loc) · 2.48 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* This file is only used for demo purpose.
*
* @package Validator
* @author Ravi Kumar
* @version 0.1.0
* @copyright Copyright (c) 2014, Ravi Kumar
* @license https://github.com/ravikumar8/PHP-Validator/blob/master/LICENSE MIT
**/
require_once 'classes/Database.php';
require_once 'classes/ErrorHandler.php';
require_once 'classes/Validator.php';
$db = new Database;
$errorHandler = new ErrorHandler;
$errosHtml = '';
if(!empty($_POST)) {
$validator = new Validator($db, $errorHandler);
$validation = $validator->check($_POST, [
'username' => [
'required' => true,
'maxlength' => 20,
'minlength' => 3,
'alnum' => true,
'unique' => 'users'
],
'email' => [
'required' => true,
'maxlength' => 255,
'email' => true,
'unique' => 'users'
],
'password' => [
'required' => true,
'minlength' => 7
],
'password_again' => [
'matches' => 'password'
]
]);
if( $validation->fails() ) {
//echo '<pre>', print_r( $validation, 1 ), '</pre>';
if( $validation->errors()->hasErrors('username') ) {
$errosHtml = '<li>' . implode( '</li><li>' , $validation->errors()->all('username') ) . '</li>';
}
if( $validation->errors()->hasErrors('email') ) {
$errosHtml .= '<li>' . implode( '</li><li>' , $validation->errors()->all('email') ) . '</li>';
}
if( $validation->errors()->hasErrors('password') ) {
$errosHtml .= '<li>' . implode( '</li><li>' , $validation->errors()->all('password') ) . '</li>';
}
if( $validation->errors()->hasErrors('password_again') ) {
$errosHtml .= '<li>' . implode( '</li><li>' , $validation->errors()->all('password_again') ) . '</li>';
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Validation</title>
<link rel="stylesheet" href="assets/css/main.css">
</head>
<body>
<div class="container">
<?php if( $errosHtml ) { ?>
<ul class="alert error">
<?php echo $errosHtml; ?>
</ul>
<?php } ?>
<form action="index.php" method="post" auto-fill="off">
<ul>
<li>Username:<input type="text" name="username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : ''; ?>"></li>
<li>Email:<input type="email" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>"></li>
<li>Password:<input type="password" name="password"></li>
<li>Password again:<input type="password" name="password_again"></li>
<li><input type="submit" class="btn"></li>
</ul>
</form>
</div>
</body>
</html>