This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
/
manual-rendering.php
235 lines (183 loc) Β· 5.53 KB
/
manual-rendering.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* Nette\Forms manual form rendering.
*
* - separated form and rules definition
* - manual form rendering
*/
require_once __DIR__ . '/../libs/Nette/Nette/loader.php';
use Nette\Forms\Form,
Nette\Debug;
Debug::enable();
$countries = array(
'Select your country',
'Europe' => array(
'CZ' => 'Czech Republic',
'SK' => 'Slovakia',
'GB' => 'United Kingdom',
),
'CA' => 'Canada',
'US' => 'United States',
'?' => 'other',
);
$sex = array(
'm' => 'male',
'f' => 'female',
);
// Step 1: Define form
$form = new Form;
$form->addText('name');
$form->addText('age');
$form->addRadioList('gender', NULL, $sex);
$form->addText('email')->setEmptyValue('@');
$form->addCheckbox('send');
$form->addText('street');
$form->addText('city');
$form->addSelect('country', NULL, $countries)->skipFirst();
$form->addPassword('password');
$form->addPassword('password2');
$form->addFile('avatar');
$form->addHidden('userid');
$form->addTextArea('note');
$form->addSubmit('submit');
// Step 1b: Define validation rules
$form['name']->addRule(Form::FILLED, 'Enter your name');
$form['age']->addRule(Form::FILLED, 'Enter your age');
$form['age']->addRule(Form::INTEGER, 'Age must be numeric value');
$form['age']->addRule(Form::RANGE, 'Age must be in range from %d to %d', array(10, 100));
// conditional rule: if is email filled, ...
$form['email']->addCondition(Form::FILLED)
->addRule(Form::EMAIL, 'Incorrect E-mail Address'); // ... then check email
// another conditional rule: if is checkbox checked...
$form['send']->addCondition(Form::EQUAL, TRUE)
// toggle div #sendBox
->toggle('sendBox');
$form['city']->addConditionOn($form['send'], Form::EQUAL, TRUE)
->addRule(Form::FILLED, 'Enter your shipping address');
$form['country']->addConditionOn($form['send'], Form::EQUAL, TRUE)
->addRule(Form::FILLED, 'Select your country');
$form['password']->addRule(Form::FILLED, 'Choose your password');
$form['password']->addRule(Form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3);
$form['password2']->addConditionOn($form['password'], Form::VALID)
->addRule(Form::FILLED, 'Reenter your password')
->addRule(Form::EQUAL, 'Passwords do not match', $form['password']);
// Step 2: Check if form was submitted?
if ($form->isSubmitted()) {
// Step 2c: Check if form is valid
if ($form->isValid()) {
echo '<h2>Form was submitted and successfully validated</h2>';
Debug::dump($form->values);
// this is the end, my friend :-)
if (empty($disableExit)) exit;
}
} else {
// not submitted, define default values
$defaults = array(
'name' => 'John Doe',
'userid' => 231,
'country' => 'CZ', // Czech Republic
);
$form->setDefaults($defaults);
}
// Step 3: Render form
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Nette\Forms manual form rendering | Nette Framework</title>
<style type="text/css">
.required {
color: darkred
}
fieldset {
padding: .5em;
margin: .5em 0;
background: #E4F1FC;
border: 1px solid #B2D1EB;
}
input.button {
font-size: 120%;
}
th {
width: 10em;
text-align: right;
}
</style>
<link rel="stylesheet" type="text/css" media="screen" href="files/style.css" />
<script src="netteForms.js"></script>
</head>
<body>
<h1>Nette\Forms manual form rendering</h1>
<?php $form->render('begin') ?>
<?php if ($form->errors): ?>
<p>Opravte chyby:</p>
<?php $form->render('errors') ?>
<?php endif ?>
<fieldset>
<legend>Personal data</legend>
<table>
<tr class="required">
<th><?php echo $form['name']->getLabel('Your name:') ?></th>
<td><?php echo $form['name']->control->cols(35) ?></td>
</tr>
<tr class="required">
<th><?php echo $form['age']->getLabel('Your age:') ?></th>
<td><?php echo $form['age']->control->cols(5) ?></td>
</tr>
<tr>
<th><?php echo $form['gender']->getLabel('Your gender:') ?></th>
<td><?php echo $form['gender']->control ?></td>
</tr>
<tr>
<th><?php echo $form['email']->getLabel('E-mail:') ?></th>
<td><?php echo $form['email']->control->cols(35) ?></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Shipping address</legend>
<p><?php echo $form['send']->control?><?php echo $form['send']->getLabel('Ship to address') ?></p>
<table id="sendBox">
<tr>
<th><?php echo $form['street']->getLabel('Street:') ?></th>
<td><?php echo $form['street']->control->cols(35) ?></td>
</tr>
<tr class="required">
<th><?php echo $form['city']->getLabel('City:') ?></th>
<td><?php echo $form['city']->control->cols(35) ?></td>
</tr>
<tr class="required">
<th><?php echo $form['country']->getLabel('Country:') ?></th>
<td><?php echo $form['country']->control ?></td>
</tr>
</table>
</fieldset>
<fieldset>
<legend>Your account</legend>
<table>
<tr class="required">
<th><?php echo $form['password']->getLabel('Choose password:') ?></th>
<td><?php echo $form['password']->control->cols(20) ?></td>
</tr>
<tr class="required">
<th><?php echo $form['password2']->getLabel('Reenter password:') ?></th>
<td><?php echo $form['password2']->control->cols(20) ?></td>
</tr>
<tr>
<th><?php echo $form['avatar']->getLabel('Picture:') ?></th>
<td><?php echo $form['avatar']->control ?></td>
</tr>
<tr>
<th><?php echo $form['note']->getLabel('Comment:') ?></th>
<td><?php echo $form['note']->control->cols(30)->rows(5) ?></td>
</tr>
</table>
</fieldset>
<div>
<?php echo $form['userid']->control ?>
<?php echo $form['submit']->getControl('Send') ?>
</div>
<?php $form->render('end'); ?>
</body>
</html>