-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsample.php
More file actions
116 lines (100 loc) · 3.93 KB
/
sample.php
File metadata and controls
116 lines (100 loc) · 3.93 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
require 'Form.php';
assert(version_compare(PHP_VERSION, '5.4', '>='), '
PHP 5.4 required
This sample make uses 5.4 for the closure $this support, always available <?=,
short array syntax and built-in web server for convenience only. 5.4 is not
required to use Form itself, in fact I think it could go as way back as 5.0.');
// Defining the form using a class because it's actually cool to name forms.
// But we could have also pass the fields to the constructor.
class RegistrationForm extends Form {
public function init() {
$this->fields = [
'email' =>
function($value) {
Form::check(!empty($value), 'Email is required');
Form::check(filter_var($value, FILTER_VALIDATE_EMAIL), 'Invalid email');
},
'password' =>
function($value) {
Form::check(strlen($value) >= 6, 'Password must be at least 6 characters');
},
'password_confirmation' =>
function($value) {
Form::check($this->password == $value, 'Password confirmation must match');
},
'picture' =>
function($value) {
Form::check($value['error'] != UPLOAD_ERR_NO_FILE, 'Picture is required');
Form::check($value['size'] < 307200, 'Please upload a picture less than 300k');
Form::check(in_array($value['type'], ['image/gif', 'image/jpeg', 'image/png']),
'Uploaded picture must be a gif, jpeg or png');
}
];
}
}
// Do this in your controller or something
$form = new RegistrationForm($_POST + $_FILES);
?>
<?php if ($form->errors): ?>
<ul class="errors">
<?php foreach ($form->errors as $key => $message): ?>
<li><?= htmlspecialchars($message) ?></li>
<?php endforeach ?>
</ul>
<?php elseif (!empty($form->values)): ?>
<div>
Email: <?= $form->email ?><br>
Password: <?= $form->password ?><br>
Picture: <?= $form->picture['name'] ?><br>
</div>
<?php endif ?>
<form action="" method="post" enctype="multipart/form-data">
<div class="<?= $form->error('email', 'error') ?>">
<label>Email</label>
<div>
<input type="text" id="email" name="email" value="<?= htmlspecialchars($form->email) ?>">
</div>
<?php if ($form->error('email')): ?>
<div class="error">
<?= htmlspecialchars($form->error('email')) ?>
</div>
<?php endif ?>
</div>
<div class="<?= $form->error('password', 'error') ?>">
<label>Password</label>
<div>
<input type="password" id="password" name="password" value="<?= htmlspecialchars($form->password) ?>">
</div>
<?php if ($form->error('password')): ?>
<div class="error">
<?= htmlspecialchars($form->error('password')) ?>
</div>
<?php endif ?>
</div>
<div class="<?= $form->error('password_confirmation', 'error') ?>">
<label>Confirm</label>
<div>
<input type="password" id="password_confirmation" name="password_confirmation" value="<?= htmlspecialchars($form->password_confirmation) ?>">
</div>
<?php if ($form->error('password_confirmation')): ?>
<div class="error">
<?= htmlspecialchars($form->error('password_confirmation')) ?>
</div>
<?php endif ?>
</div>
<div class="<?= $form->error('picture', 'error') ?>">
<label>Picture</label>
<div>
<input type="file" id="picture" name="picture">
</div>
<?php if ($form->error('picture')): ?>
<div class="error">
<?= htmlspecialchars($form->error('picture')) ?>
</div>
<?php endif ?>
</div>
<div>
<input type="submit" value="Submit">
</div>
</form>