forked from mindplay-dk/kissform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.php
148 lines (113 loc) · 3.99 KB
/
demo.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
use mindplay\kissform\Facets\TokenServiceInterface;
use mindplay\kissform\Fields\CheckboxField;
use mindplay\kissform\Fields\HiddenField;
use mindplay\kissform\Fields\IntField;
use mindplay\kissform\Fields\TextField;
use mindplay\kissform\Fields\TokenField;
use mindplay\kissform\Framework\SessionTokenStore;
use mindplay\kissform\Framework\TokenService;
use mindplay\kissform\InputModel;
use mindplay\kissform\InputRenderer;
use mindplay\kissform\InputValidation;
use mindplay\kissform\Validators\CheckToken;
require dirname(__DIR__) . '/vendor/autoload.php';
session_start();
class DonationForm
{
/** @var HiddenField */
public $token;
/** @var TextField */
public $first_name;
/** @var TextField */
public $last_name;
/** @var IntField */
public $amount;
/** @var CheckboxField */
public $i_agree;
public function __construct(TokenServiceInterface $token_service)
{
$this->token = new TokenField(self::class, $token_service);
$this->first_name = new TextField('first_name');
$this->first_name->setLabel('First Name');
$this->first_name->setRequired();
$this->last_name = new TextField('last_name');
$this->last_name->setLabel('Last Name');
$this->last_name->setRequired();
$this->amount = new IntField('amount');
$this->amount->setRequired();
$this->amount->setLabel('Donation Amount');
$this->amount->min_value = 20;
$this->amount->max_value = 1000;
$this->amount->setPlaceholder('$20 up to $1,000');
$this->i_agree = new CheckboxField('i_agree');
$this->i_agree->setLabel('I Agree to Donate');
$this->i_agree->setRequired();
}
}
$token_service = new TokenService(new SessionTokenStore(), 'SuPeR_sEcReT_KeY');
$t = new DonationForm($token_service);
$model = InputModel::create(@$_SESSION[__FILE__]);
if (isset($_POST['form'])) {
$model = InputModel::create($_POST['form']);
$_SESSION[__FILE__] = $model;
$validator = new InputValidation($model);
$validator->check([
$t->token,
$t->first_name,
$t->last_name,
$t->amount,
$t->i_agree,
]);
if ($model->isValid()) {
$message = 'Thank You for your Donation!';
unset($_SESSION[__FILE__]);
} else {
header('Location: ' . $_SERVER['REQUEST_URI']);
exit;
}
unset($validator);
}
$form = new InputRenderer($model, 'form');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Donation Form</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/>
<style>
/* note that bootstrap's checkbox/radio markup prevents styling - see: https://github.com/twbs/bootstrap/issues/19931 */
.radio input[type=radio], .radio-inline input[type=radio], .checkbox input[type=checkbox], .checkbox-inline input[type=checkbox] {
margin-left: 0;
}
</style>
</head>
<body>
<div class="container">
<h1>Make a Donation</h1>
<?php if (isset($message)): ?>
<div class="alert alert-success"><?= $message ?></div>
<p>» <a href="<?= basename(__FILE__) ?>">Donate Again!</a></p>
<?php else: ?>
<form method="post">
<?= $form->render($t->token) ?>
<?= $form->renderGroup($t->first_name) ?>
<?= $form->renderGroup($t->last_name) ?>
<?= $form->renderGroup($t->amount) ?>
<?= $form->render($t->i_agree) ?>
<input class="btn btn-lg btn-primary" type="submit" value="Donate" />
</form>
<?php endif ?>
<?php if ($model->hasErrors()): ?>
<div class="alert alert-danger">
<ul>
<?php foreach ($model->getErrors() as $error): ?>
<li><?= $error ?></li>
<?php endforeach ?>
</ul>
</div>
<?php endif ?>
</div>
</body>
</html>