-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.html
More file actions
470 lines (411 loc) · 15.7 KB
/
validation.html
File metadata and controls
470 lines (411 loc) · 15.7 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Validation Class - Introduction - Fuel Documentation</title>
<link href="../../assets/css/main.css" media="screen" rel="stylesheet" />
<script type="text/javascript" src="../../assets/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../../assets/js/nav.js"></script>
<script type="text/javascript" src="../../assets/js/highlight.pack.js"></script>
<script type="text/javascript">
$(function() {
show_nav('classes', '../../');
});
hljs.tabReplace = ' ';
hljs.initHighlightingOnLoad();
</script>
</head>
<body>
<header>
<h1>Fuel Documentation</h1>
</header>
<div id="main-nav"></div>
<section id="content">
<h2>Validation Class</h2>
<p>The Validation class helps you validate user input, if you want to create a form & its validation at
the same time use the <a href="fieldset.html">Fieldset</a> class instead.</p>
<ul>
<li><a href="#config">Configuration</a></li>
<li><a href="#usage">Usage</a></li>
<li><a href="#rules">Validation rules</a></li>
<li><a href="#errors">Error messages</a></li>
<li><a href="methods.html">Methods</a></li>
</ul>
<article>
<h3><a name="config">Configuration</a></h3>
<p>Optionally, the validation class can be configured through the global application configuration file, <strong>app/config/config.php</strong>. Define a section called '<strong>validation</strong>', in which the following settings can be defined:</p>
<table class="method">
<tbody>
<tr>
<th>Variable</th>
<th>Type</th>
<th>Default</th>
<th class="description">Description</th>
</tr>
<tr>
<th><kbd>no_errors</kbd></th>
<td>string</td>
<td><pre class="php"><code>''</code></pre></td>
<td>String to return if no validation errors were discovered.</td>
</tr>
<tr>
<th><kbd>open_list</kbd></th>
<td>string</td>
<td><pre class="php"><code>'<ul>'</code></pre></td>
<td>String to be prepended to the list of errors. Usually this is some form of HTML to format the list. By default, it's formatted as an unordered list.</td>
</tr>
<tr>
<th><kbd>close_list</kbd></th>
<td>string</td>
<td><pre class="php"><code>'</ul>'</code></pre></td>
<td>String to be appended to the list of errors.</td>
</tr>
<tr>
<th><kbd>open_error</kbd></th>
<td>string</td>
<td><pre class="php"><code>'<li>'</code></pre></td>
<td>String to be prepended to each individual error message.</td>
</tr>
<tr>
<th><kbd>close_error</kbd></th>
<td>string</td>
<td><pre class="php"><code>'</li>'</code></pre></td>
<td>String to be appended to each individual error message.</td>
</tr>
<tr>
<th><kbd>quote_label</kbd></th>
<td>boolean</td>
<td><pre class="php"><code>false</code></pre></td>
<td>If true, and the label of the validated field contains spaces, the label will be enclosed in double quotes for enhanced readability.</td>
</tr>
</tbody>
</table>
<p>If one or more of these values are missing from the global configuration, the class will use the defaults as defined in this table.</p>
</article>
<article>
<h3><a name="usage">Usage</a></h3>
<p>To start validation you need to create an object, this can be the default object named "default" or
you can name it if you need multiple validation objects.</p>
<pre class="php"><code>// Use default
$val = Validation::factory();
// ... or name it
$val = Validation::factory('my_validation');</code></pre>
<p>After having it instantiated you can start adding fields to it. This works exactly like the when using
the Fieldset class, however here we'll only document the preferred usage.</p>
<pre class="php"><code>$val = Validation::factory('my_validation');
// Add a field for username, give it the label "Your username" and make it required
$val->add('username', 'Your username')->add_rule('required');
// Now add another field for password, and require it to contain at least 3 and at most 10 characters
$val->add('password', 'Your password')->add_rule('required')
->add_rule('min_length', 3)
->add_rule('max_length', 10);</code></pre>
<p>The first parameter of the add_rule() method can contain PHP native function names, any valid PHP
callback and Closures in addition to the provided validation methods. The method will get the value to
be validated as its first argument and any further arguments can be given to the add_rule() method.</p>
<p>We also provide a shorter syntax which is very limited in comparison. It will not accept array-callbacks,
closures or parameters other then strings.</p>
<pre class="php"><code>// The same fields as the example above
$val = Validation::factory('my_validation');
$val->add_field('username', 'Your username', 'required');
$val->add_field('password', 'Your password', 'required|min_length[3]|max_length[10]');</code></pre>
<p>Once all the fields have been added you can run your validation. This will default to $_POST input, but
can be extended and overwritten when given an input array.</p>
<pre class="php"><code>// run validation on just post
if ($val->run())
{
// process your stuff when validation succeeds
}
else
{
// validation failed
}
// alternative to above, overwriting the username in post, password will still be sought in post
if ($val->run(array('username' => 'something')))</code></pre>
<p>When validation is ran there are three methods available for information about the input:</p>
<pre class="php"><code>// get an array of successfully validated fields => value pairs
$vars = $val->validated();
// get an array of validation errors as field => error pairs
$errors = $val->errors();
// get an array of the input that was validated, this merged the post & input given to run()
$input = $val->input();
// all these methods can also get just the value for a single field
$var = $val->validated('username');</code></pre>
<p>Validation can also run partially, in that case even required fields are ignored when they're not in
POST or the input given to run(). One does this by setting the 2nd parameter of run to true.</p>
<pre class="php"><code>// this will only validate the password when username isn't present in post
$val->run(array('password' => 'whatever'), true);</code></pre>
</article>
<article>
<h3><a name="rules">Validation rules</a></h3>
<p>Note that all methods (even min_length) will also return true on empty input. To also require the field
you must add the rule "required" as well.</p>
<p>All these rules can be used like below:</p>
<pre class="php"><code>// example normal usage with rule without and one with params
$val->add('email', 'Email address')->add_rule('match_value', '[email protected]', true)->add_rule('valid_email');
$val->add_field('email', 'Email address', 'match_value[[email protected],1]|valid_email');</code></pre>
<h4 id="rules_table">Rules table</h4>
<table class="config">
<tbody>
<tr class="header">
<th>Rule</th>
<th>Additional parameters</th>
<th>Description</th>
</tr>
<tr>
<th>required</th>
<td>(none)</td>
<td>
The field must be set and have been given something other than <kbd>null</kbd>,
<kbd>false</kbd> or empty string.
</td>
</tr>
<tr>
<th>match_value</th>
<td><kbd>$compare</kbd>, <kbd>$strickt</kbd> = false</td>
<td>
The field input must match <kbd>$compare</kbd>, will be done using == unless 2nd parameter
is also given as true (then === is used).
</td>
</tr>
<tr>
<th>match_pattern</th>
<td><kbd>$pattern</kbd></td>
<td>
Will try to match the value against the given $pattern which must be a full PREG regex.
</td>
</tr>
<tr>
<th>match_field</th>
<td><kbd>$field</kbd></td>
<td>
Will try to match the field to the field with the given fieldname, the matching is done
using ===.<br />
<strong>Important:</strong> you can only match against a field that was added before the
field this rule is added to.
</td>
</tr>
<tr>
<th>min_length</th>
<td><kbd>$length</kbd></td>
<td>
Tests whether the string contains at least <kbd>$length</kbd>'s number of character.
</td>
</tr>
<tr>
<th>max_length</th>
<td><kbd>$length</kbd></td>
<td>
Tests whether the string contains no more than <kbd>$length</kbd>'s number of character.
</td>
</tr>
<tr>
<th>exact_length</th>
<td><kbd>$length</kbd></td>
<td>
Tests whether the string has precisely <kbd>$length</kbd>'s number of character.
</td>
</tr>
<tr>
<th>valid_email</th>
<td>(none)</td>
<td>
Validates if the given input is a valid email address.
</td>
</tr>
<tr>
<th>valid_emails</th>
<td>(none)</td>
<td>
Validates multiple email addresses separated by commas.
</td>
</tr>
<tr>
<th>valid_url</th>
<td>(none)</td>
<td>
Validates if the given input is a valid URL.
</td>
</tr>
<tr>
<th>valid_ip</th>
<td>(none)</td>
<td>
Validates if the given input is a valid IP.
</td>
</tr>
<tr>
<th>numeric_min</th>
<td><kbd>$min_val</kbd></td>
<td>
Tests whether the given input is a number that is greater than <kbd>$min_val</kbd>, it does
not check or cast the input to a numeric value so any non-numeric value will be considered
to be zero. Use the PHP function <kbd>is_numeric</kbd> to check that first.
</td>
</tr>
<tr>
<th>numeric_max</th>
<td><kbd>$max_val</kbd></td>
<td>
Tests whether the given input is a number that is smaller than <kbd>$max_val</kbd>. (see
note about non-numeric values with <kbd>numeric_min</kbd> rule)
</td>
</tr>
<tr>
<th>valid_string</th>
<td><kbd>$flags</kbd> = array('alpha', 'utf8')</td>
<td>
See below.
</td>
</tr>
</tbody>
</table>
<h4 id="valid_string_rule">Valid string rule</h4>
<p>Validates whether a string adheres to the conditions set by the $flags parameter. The accepted flags
are:</p>
<table class="config">
<tbody>
<tr class="header">
<th>Flag</th>
<th>Description</th>
</tr>
<tr>
<th>alpha</th>
<td>Allow alphabetical characters.</td>
</tr>
<tr>
<th>uppercase</th>
<td>Used in combination with alpha to only allow uppercase characters.</td>
</tr>
<tr>
<th>lowercase</th>
<td>Used in combination with alpha to only allow lowercase characters.</td>
</tr>
<tr>
<th>numeric</th>
<td>Allow numeric characters.</td>
</tr>
<tr>
<th>spaces</th>
<td>Allow normal spaces.</td>
</tr>
<tr>
<th>newlines</th>
<td>Allow newline character.</td>
</tr>
<tr>
<th>tabs</th>
<td>Allow tabs.</td>
</tr>
<tr>
<th>dots</th>
<td>Allow dots.</td>
</tr>
<tr>
<th>punctuation</th>
<td>Allow dots, commas, exclamation marks, question marks, colons and semi-colons.</td>
</tr>
<tr>
<th>dashes</th>
<td>Allow dashes and underscores.</td>
</tr>
<tr>
<th>utf8</th>
<td>Adds UTF8 modifier to regex.</td>
</tr>
</tbody>
</table>
</article>
<article>
<h3><a name="errors">Error messages</a></h3>
<p class="note">Errors are returned as Validation_Error objects which you can work with for more flexible
output, if you don't need that just cast the Validation_Error object to a string to get the error message.</p>
<p>Error messages are set using a language file "validation.php" which is loaded automatically.</p>
<p>There are 2 ways of manipulating the error messages during validation.</p>
<pre class="php"><code>// change a rule per validation object
$val->set_message('required', 'The field :label is required.');
// or per error
echo $val->errors('username')->get_message('The field :label must be filled out before auth is attempted.');
// Outputs "The field Your username must be filled out before auth is attempted." when validation for username
// failed, or nothing when not yet validated or validation succeeded.</code></pre>
<p>As you may have noticed the error messages accept a couple of variables that are replaced by the
field's properties. Below is a list of the available variables:</p>
<table class="config">
<tbody>
<tr class="header">
<th>Variable</th>
<th>Description</th>
</tr>
<tr>
<th>:field</th>
<td>Will be substituted by the field's name.</td>
</tr>
<tr>
<th>:label</th>
<td>Will be substituted by the field's label.</td>
</tr>
<tr>
<th>:value</th>
<td>Will be substituted by the field's value which could not be validated.</td>
</tr>
<tr>
<th>:rule</th>
<td>Will be substituted by the rule that failed. The included rules will be just their name,
others will be the string callback or classname:method (which will also be the key for which
you must add a validation rule if you want to).</td>
</tr>
<tr>
<th>:param:1</th>
<td>Will be replaced by the first additional parameter's value, :param:2 will be the second,
etc.</td>
</tr>
</tbody>
</table>
<h3>Naming callback/closure rules</h3>
<p>Full callback rules or closures can be named as well, by default their names will be:</p>
<ul>
<li><strong>Closure:</strong> "closure"</li>
<li><strong>array($class, $method):</strong> "$class:$method"</li>
<li><strong>array($object, $method):</strong> get_class($object).":$method"</li>
</ul>
<p>If you want to give them custom names instead you can do that like this:</p>
<pre class="php"><code>// Add a rule which checks if the input is odd, the expected message name will be 'validation.odd'
$field->add_rule(array('odd' => function($val) { return (bool) ($val % 2); }));
</code></pre>
</article>
<article>
<h3><a name="methods">Extending Validation class</a></h3>
<p>There are few approaches to extend the validation class:</p>
<p>1. To extend the core class like described in the <a href="../general/extending_core.html">Extending Core Classes</a></p>
<p>2. To create a class in <b>app/classes/myvalidation.php</b> (for example)</p>
<pre class="php"><code>// app/classes/myvalidation.php
class Myvalidation {
public function _validation_unique($val, $options)
{
list($table, $field) = explode('.', $options);
$result = DB::select("LOWER (\"$field\")")
->where($field, '=', Str::lower($val))
->from($table)->execute();
return ! ($result->count() > 0);
}
}
// and call it like:
$val = Validation::factory();
$val->add_callable('myvalidation'); // here we add the class to load rules from
$val->add_field('username', 'Your username', 'trim|strip_tags|required|unique[users.username]');</code></pre>
<p>3. Calling callbacks from a model. It works like a method described above, but we only need to call it in other way:</p>
<pre class="php"><code>$val = Validation::factory();
$val->add_model('Model_User');</code></pre>
<p class="note">
<strong>Note: </strong>
You need the '_validation_' prefix for a method to be used in validation.
</p>
</article>
</section>
<section id="footer">
<p>
<a href="http://fuelphp.com">Fuel</a> is released under the MIT license.<br />
© 2010 - 2011 Fuel Development Team
</p>
</section>
</body>
</html>