blob: 18f391a57d9a703851e2a12ec3ea2ea04533b822 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<title>Form Validation : CodeIgniter User Guide</title>
7
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11<script type="text/javascript" src="../nav/nav.js"></script>
12<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13<script type="text/javascript" src="../nav/moo.fx.js"></script>
14<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='ExpressionEngine Dev Team' />
20<meta name='description' content='CodeIgniter User Guide' />
21</head>
22<body>
23
24<!-- START NAVIGATION -->
25<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
26<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
27<div id="masthead">
28<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
29<tr>
Derek Jones8917af72010-03-05 12:41:45 -060030<td><h1>CodeIgniter User Guide Version 2.0.0</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000031<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
32</tr>
33</table>
34</div>
35<!-- END NAVIGATION -->
36
37
38<!-- START BREADCRUMB -->
39<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
40<tr>
41<td id="breadcrumb">
42<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
43<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
44Form Validation
45</td>
46<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
47</tr>
48</table>
49<!-- END BREADCRUMB -->
50
51<br clear="all" />
52
53
54<!-- START CONTENT -->
55<div id="content">
56
57<h1>Form Validation</h1>
58
59<p>CodeIgniter provides a comprehensive form validation and data prepping class that helps minimize the amount of code you'll write.</p>
60
Derek Allard2067d1a2008-11-13 22:59:24 +000061<ul>
62<li><a href="#overview">Overview</a></li>
63<li><a href="#tutorial">Form Validation Tutorial</a>
64
65 <ul>
66 <li><a href="#theform">The Form</a></li>
67 <li><a href="#thesuccesspage">The Success Page</a></li>
68 <li><a href="#thecontroller">The Controller</a></li>
69 <li><a href="#validationrules">Setting Validation Rules</a></li>
70 <li><a href="#validationrulesasarray">Setting Validation Rules Using an Array</a></li>
71 <li><a href="#cascadingrules">Cascading Rules</a></li>
72 <li><a href="#preppingdata">Prepping Data</a></li>
73 <li><a href="#repopulatingform">Re-populating the Form</a></li>
74 <li><a href="#callbacks">Callbacks</a></li>
75 <li><a href="#settingerrors">Setting Error Messages</a></li>
76 <li><a href="#errordelimiters">Changing the Error Delimiters</a></li>
77 <li><a href="#translatingfn">Translating Field Names</a></li>
78 <li><a href="#individualerrors">Showing Errors Individually</a></li>
79 <li><a href="#savingtoconfig">Saving Sets of Validation Rules to a Config File</a></li>
80 <li><a href="#arraysasfields">Using Arrays as Field Names</a></li>
81 </ul>
82</li>
83<li><a href="#rulereference">Rule Reference</a></li>
84<li><a href="#preppingreference">Prepping Reference</a></li>
85<li><a href="#functionreference">Function Reference</a></li>
86<li><a href="#helperreference">Helper Reference</a></li>
87
88</ul>
89
90
91
92
93
94
95<p>&nbsp;</p>
96
97<a name="overview"></a>
98<h1>Overview</h1>
99
100
101<p>Before explaining CodeIgniter's approach to data validation, let's describe the ideal scenario:</p>
102
103<ol>
104<li>A form is displayed.</li>
105<li>You fill it in and submit it.</li>
Barry Mienydd671972010-10-04 16:33:58 +0200106<li>If you submitted something invalid, or perhaps missed a required item, the form is redisplayed containing your data
Derek Allard2067d1a2008-11-13 22:59:24 +0000107along with an error message describing the problem.</li>
108<li>This process continues until you have submitted a valid form.</li>
109</ol>
110
111<p>On the receiving end, the script must:</p>
112
113<ol>
114<li>Check for required data.</li>
115<li>Verify that the data is of the correct type, and meets the correct criteria. For example, if a username is submitted
116it must be validated to contain only permitted characters. It must be of a minimum length,
117and not exceed a maximum length. The username can't be someone else's existing username, or perhaps even a reserved word. Etc.</li>
118<li>Sanitize the data for security.</li>
119<li>Pre-format the data if needed (Does the data need to be trimmed? HTML encoded? Etc.)</li>
120<li>Prep the data for insertion in the database.</li>
121</ol>
122
123
124<p>Although there is nothing terribly complex about the above process, it usually requires a significant
125amount of code, and to display error messages, various control structures are usually placed within the form HTML.
126Form validation, while simple to create, is generally very messy and tedious to implement.</p>
127
128<p>&nbsp;</p>
129
130
131<a name="tutorial"></a>
132<h1>Form Validation Tutorial</h1>
133
134<p>What follows is a "hands on" tutorial for implementing CodeIgniters Form Validation.</p>
135
136
137<p>In order to implement form validation you'll need three things:</p>
138
139<ol>
140<li>A <a href="../general/views.html">View</a> file containing a form.</li>
141<li>A View file containing a "success" message to be displayed upon successful submission.</li>
142<li>A <a href="../general/controllers.html">controller</a> function to receive and process the submitted data.</li>
143</ol>
144
145<p>Let's create those three things, using a member sign-up form as the example.</p>
146
147
148
149<a name="theform"></a>
150
151<h2>The Form</h2>
152
153<p>Using a text editor, create a form called <dfn>myform.php</dfn>. In it, place this code and save it to your <samp>applications/views/</samp>
154folder:</p>
155
156
157<textarea class="textarea" style="width:100%" cols="50" rows="30">&lt;html>
158&lt;head>
159&lt;title>My Form&lt;/title>
160&lt;/head>
161&lt;body>
162
163&lt;?php echo validation_errors(); ?>
164
165&lt;?php echo form_open('form'); ?>
166
167&lt;h5>Username&lt;/h5>
168&lt;input type="text" name="username" value="" size="50" />
169
170&lt;h5>Password&lt;/h5>
171&lt;input type="text" name="password" value="" size="50" />
172
173&lt;h5>Password Confirm&lt;/h5>
174&lt;input type="text" name="passconf" value="" size="50" />
175
176&lt;h5>Email Address&lt;/h5>
177&lt;input type="text" name="email" value="" size="50" />
178
179&lt;div>&lt;input type="submit" value="Submit" />&lt;/div>
180
181&lt;/form>
182
183&lt;/body>
184&lt;/html>
185</textarea>
186
187
188
189
190<a name="thesuccesspage"></a>
191<h2>The Success Page</h2>
192
193
194<p>Using a text editor, create a form called <dfn>formsuccess.php</dfn>. In it, place this code and save it to your <samp>applications/views/</samp>
195folder:</p>
196
197
198<textarea class="textarea" style="width:100%" cols="50" rows="14">
199&lt;html>
200&lt;head>
201&lt;title>My Form&lt;/title>
202&lt;/head>
203&lt;body>
204
205&lt;h3>Your form was successfully submitted!&lt;/h3>
206
207&lt;p>&lt;?php echo anchor('form', 'Try it again!'); ?>&lt;/p>
208
209&lt;/body>
210&lt;/html>
211</textarea>
212
213
214
215<a name="thecontroller"></a>
216<h2>The Controller</h2>
217
218<p>Using a text editor, create a controller called <dfn>form.php</dfn>. In it, place this code and save it to your <samp>applications/controllers/</samp>
219folder:</p>
220
221
222<textarea class="textarea" style="width:100%" cols="50" rows="21">&lt;?php
223
224class Form extends Controller {
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 function index()
227 {
228 $this->load->helper(array('form', 'url'));
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 $this->load->library('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 if ($this->form_validation->run() == FALSE)
233 {
234 $this->load->view('myform');
235 }
236 else
237 {
238 $this->load->view('formsuccess');
239 }
240 }
241}
242?></textarea>
243
244
245<h2>Try it!</h2>
246
247<p>To try your form, visit your site using a URL similar to this one:</p>
248
249<code>example.com/index.php/<var>form</var>/</code>
250
251<p><dfn>If you submit the form you should simply see the form reload. That's because you haven't set up any validation
252rules yet.</dfn></p>
253
254<p><strong>Since you haven't told the Form Validation class to validate anything yet, it returns <kbd>FALSE</kbd> (boolean false) by default. The <samp>run()</samp>
255function only returns <kbd>TRUE</kbd> if it has successfully applied your rules without any of them failing.</strong></p>
256
257
258<h2>Explanation</h2>
259
260<p>You'll notice several things about the above pages:</p>
261
262<p>The <dfn>form</dfn> (myform.php) is a standard web form with a couple exceptions:</p>
263
264<ol>
265<li>It uses a <dfn>form helper</dfn> to create the form opening.
266Technically, this isn't necessary. You could create the form using standard HTML. However, the benefit of using the helper
267is that it generates the action URL for you, based on the URL in your config file. This makes your application more portable in the event your URLs change.</li>
268
269<li>At the top of the form you'll notice the following function call:
270<code>&lt;?php echo validation_errors(); ?&gt;</code>
271
272<p>This function will return any error messages sent back by the validator. If there are no messages it returns an empty string.</p>
273</li>
274</ol>
275
276<p>The <dfn>controller</dfn> (form.php) has one function: <dfn>index()</dfn>. This function initializes the validation class and
277loads the <var>form helper</var> and <var>URL helper</var> used by your view files. It also <samp>runs</samp>
278the validation routine. Based on
279whether the validation was successful it either presents the form or the success page.</p>
280
281
282
283
284<a name="validationrules"></a>
285
286<h2>Setting Validation Rules</h2>
287
288<p>CodeIgniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data
289at the same time. To set validation rules you will use the <dfn>set_rules()</dfn> function:</p>
290
291<code>$this->form_validation->set_rules();</code>
292
293<p>The above function takes <strong>three</strong> parameters as input:</p>
294
295<ol>
296 <li>The field name - the exact name you've given the form field.</li>
297 <li>A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username". <strong>Note:</strong> If you would like the field name to be stored in a language file, please see <a href="#translatingfn">Translating Field Names</a>.</li>
298 <li>The validation rules for this form field.</li>
299</ol>
300
301
302<p><br />Here is an example. In your <dfn>controller</dfn> (form.php), add this code just below the validation initialization function:</p>
303
304<code>
305$this->form_validation->set_rules('username', 'Username', 'required');<br />
306$this->form_validation->set_rules('password', 'Password', 'required');<br />
307$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');<br />
308$this->form_validation->set_rules('email', 'Email', 'required');<br />
309</code>
310
311<p>Your controller should now look like this:</p>
312
Derek Allard4b6d4932008-12-07 17:04:56 +0000313<textarea class="textarea" style="width:100%" cols="50" rows="28">&lt;?php
Derek Allard2067d1a2008-11-13 22:59:24 +0000314
315class Form extends Controller {
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 function index()
318 {
319 $this->load->helper(array('form', 'url'));
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 $this->load->library('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 $this->form_validation->set_rules('username', 'Username', 'required');
324 $this->form_validation->set_rules('password', 'Password', 'required');
325 $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
326 $this->form_validation->set_rules('email', 'Email', 'required');
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 if ($this->form_validation->run() == FALSE)
329 {
330 $this->load->view('myform');
331 }
332 else
333 {
334 $this->load->view('formsuccess');
335 }
336 }
337}
338?></textarea>
339
340<p><dfn>Now submit the form with the fields blank and you should see the error messages.
341If you submit the form with all the fields populated you'll see your success page.</dfn></p>
342
343<p class="important"><strong>Note:</strong> The form fields are not yet being re-populated with the data when
344there is an error. We'll get to that shortly.</p>
345
346
347
348
349<a name="validationrulesasarray"></a>
350<h2>Setting Rules Using an Array</h2>
351
352<p>Before moving on it should be noted that the rule setting function can be passed an array if you prefer to set all your rules in one action.
353If you use this approach you must name your array keys as indicated:</p>
354
355<code>
356$config = array(<br />
357&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
358&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'field'&nbsp;&nbsp;&nbsp;=> 'username', <br />
359&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'label'&nbsp;&nbsp;&nbsp;=> 'Username', <br />
360&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'rules'&nbsp;&nbsp;&nbsp;=> 'required'<br />
361&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />
362&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
363&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'field'&nbsp;&nbsp;&nbsp;=> 'password', <br />
364&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'label'&nbsp;&nbsp;&nbsp;=> 'Password', <br />
365&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'rules'&nbsp;&nbsp;&nbsp;=> 'required'<br />
366&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),<br />
367&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
368&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'field'&nbsp;&nbsp;&nbsp;=> 'passconf', <br />
369&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'label'&nbsp;&nbsp;&nbsp;=> 'Password Confirmation', <br />
370&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'rules'&nbsp;&nbsp;&nbsp;=> 'required'<br />
371&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;),&nbsp;&nbsp;&nbsp;<br />
372&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
373&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'field'&nbsp;&nbsp;&nbsp;=> 'email', <br />
374&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'label'&nbsp;&nbsp;&nbsp;=> 'Email', <br />
375&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'rules'&nbsp;&nbsp;&nbsp;=> 'required'<br />
376&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />
377&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
378<br />
379$this->form_validation->set_rules($config);
380</code>
381
382
383
384
385
386
387<a name="cascadingrules"></a>
388<h2>Cascading Rules</h2>
389
390<p>CodeIgniter lets you pipe multiple rules together. Let's try it. Change your rules in the third parameter of rule setting function, like this:</p>
391
392<code>
393$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]');<br />
394$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');<br />
395$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');<br />
396$this->form_validation->set_rules('email', 'Email', 'required|valid_email');<br />
397</code>
398
399<p>The above code sets the following rules:</p>
400
401<ol>
402<li>The username field be no shorter than 5 characters and no longer than 12.</li>
403<li>The password field must match the password confirmation field.</li>
404<li>The email field must contain a valid email address.</li>
405</ol>
406
407<p>Give it a try! Submit your form without the proper data and you'll see new error messages that correspond to your new rules.
408There are numerous rules available which you can read about in the validation reference.</p>
409
410
411
412<a name="preppingdata"></a>
413<h2>Prepping Data</h2>
414
415<p>In addition to the validation functions like the ones we used above, you can also prep your data in various ways.
416For example, you can set up rules like this:</p>
417
418<code>
419$this->form_validation->set_rules('username', 'Username', '<kbd>trim</kbd>|required|min_length[5]|max_length[12]|<kbd>xss_clean</kbd>');<br />
420$this->form_validation->set_rules('password', 'Password', '<kbd>trim</kbd>|required|matches[passconf]|<kbd>md5</kbd>');<br />
421$this->form_validation->set_rules('passconf', 'Password Confirmation', '<kbd>trim</kbd>|required');<br />
422$this->form_validation->set_rules('email', 'Email', '<kbd>trim</kbd>|required|valid_email');<br />
423</code>
424
425
426<p>In the above example, we are "trimming" the fields, converting the password to MD5, and running the username through
427the "xss_clean" function, which removes malicious data.</p>
428
429<p><strong>Any native PHP function that accepts one parameter can be used as a rule, like <dfn>htmlspecialchars</dfn>,
430<dfn>trim</dfn>, <dfn>MD5</dfn>, etc.</strong></p>
431
432<p><strong>Note:</strong> You will generally want to use the prepping functions <strong>after</strong>
433the validation rules so if there is an error, the original data will be shown in the form.</p>
434
435
436
437
438<a name="repopulatingform"></a>
439<h2>Re-populating the form</h2>
440
441<p>Thus far we have only been dealing with errors. It's time to repopulate the form field with the submitted data. CodeIgniter offers several helper functions
442that permit you to do this. The one you will use most commonly is:</p>
443
444<code>set_value('field name')</code>
445
446
447<p>Open your <dfn>myform.php</dfn> view file and update the <strong>value</strong> in each field using the <dfn>set_value()</dfn> function:</p>
448
Derek Allard028cbb52010-01-17 08:07:49 +0000449<p><strong>Don't forget to include each field name in the <dfn>set_value()</dfn> functions!</strong></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000450
451
452<textarea class="textarea" style="width:100%" cols="50" rows="30">
453&lt;html>
454&lt;head>
455&lt;title>My Form&lt;/title>
456&lt;/head>
457&lt;body>
458
459&lt;?php echo validation_errors(); ?>
460
461&lt;?php echo form_open('form'); ?>
462
463&lt;h5>Username&lt;/h5>
464&lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?>" size="50" />
465
466&lt;h5>Password&lt;/h5>
467&lt;input type="text" name="password" value="&lt;?php echo set_value('password'); ?>" size="50" />
468
469&lt;h5>Password Confirm&lt;/h5>
470&lt;input type="text" name="passconf" value="&lt;?php echo set_value('passconf'); ?>" size="50" />
471
472&lt;h5>Email Address&lt;/h5>
473&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?>" size="50" />
474
475&lt;div>&lt;input type="submit" value="Submit" />&lt;/div>
476
477&lt;/form>
478
479&lt;/body>
480&lt;/html>
481</textarea>
482
483
484<p><dfn>Now reload your page and submit the form so that it triggers an error. Your form fields should now be re-populated</dfn></p>
485
486<p class="important"><strong>Note:</strong> The <a href="#functionreference">Function Reference</a> section below contains functions that
487permit you to re-populate &lt;select> menus, radio buttons, and checkboxes.</p>
488
489
490<p><strong>Important Note:</strong> If you use an array as the name of a form field, you must supply it as an array to the function. Example:</p>
491
492<code>&lt;input type="text" name="<kbd>colors[]</kbd>" value="&lt;?php echo set_value('<kbd>colors[]</kbd>'); ?>" size="50" /></code>
493
494<p>For more info please see the <a href="#arraysasfields">Using Arrays as Field Names</a> section below.</p>
495
496
497
498
499
500<a name="callbacks"></a>
501<h2>Callbacks: Your own Validation Functions</h2>
502
503<p>The validation system supports callbacks to your own validation functions. This permits you to extend the validation class
504to meet your needs. For example, if you need to run a database query to see if the user is choosing a unique username, you can
505create a callback function that does that. Let's create a example of this.</p>
506
507<p>In your controller, change the "username" rule to this:</p>
508
509<code>$this->form_validation->set_rules('username', 'Username', '<kbd>callback_username_check</kbd>');</code>
510
511
512<p>Then add a new function called <dfn>username_check</dfn> to your controller. Here's how your controller should now look:</p>
513
514
Derek Allard4b6d4932008-12-07 17:04:56 +0000515<textarea class="textarea" style="width:100%" cols="50" rows="44">&lt;?php
Derek Allard2067d1a2008-11-13 22:59:24 +0000516
517class Form extends Controller {
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 function index()
520 {
521 $this->load->helper(array('form', 'url'));
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 $this->load->library('form_validation');
Barry Mienydd671972010-10-04 16:33:58 +0200524
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
526 $this->form_validation->set_rules('password', 'Password', 'required');
527 $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
528 $this->form_validation->set_rules('email', 'Email', 'required');
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 if ($this->form_validation->run() == FALSE)
531 {
532 $this->load->view('myform');
533 }
534 else
535 {
536 $this->load->view('formsuccess');
537 }
538 }
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 function username_check($str)
541 {
542 if ($str == 'test')
543 {
544 $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
545 return FALSE;
546 }
547 else
548 {
549 return TRUE;
550 }
551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553}
554?></textarea>
555
556<p><dfn>Reload your form and submit it with the word "test" as the username. You can see that the form field data was passed to your
557callback function for you to process.</dfn></p>
558
559<p><strong>To invoke a callback just put the function name in a rule, with "callback_" as the rule prefix.</strong></p>
560
561<p>You can also process the form data that is passed to your callback and return it. If your callback returns anything other than a boolean TRUE/FALSE
562it is assumed that the data is your newly processed form data.</p>
563
564
565
566
567<a name="settingerrors"></a>
568<h2>Setting Error Messages</h2>
569
570
571<p>All of the native error messages are located in the following language file: <dfn>language/english/form_validation_lang.php</dfn></p>
572
573<p>To set your own custom message you can either edit that file, or use the following function:</p>
574
575<code>$this->form_validation->set_message('<var>rule</var>', '<var>Error Message</var>');</code>
576
577<p>Where <var>rule</var> corresponds to the name of a particular rule, and <var>Error Message</var> is the text you would like displayed.</p>
578
579<p>If you include <dfn>%s</dfn> in your error string, it will be replaced with the "human" name you used for your field when you set your rules.</p>
580
581<p>In the "callback" example above, the error message was set by passing the name of the function:</p>
582
583<code>$this->form_validation->set_message('username_check')</code>
584
585<p>You can also override any error message found in the language file. For example, to change the message for the "required" rule you will do this:</p>
586
587<code>$this->form_validation->set_message('required', 'Your custom message here');</code>
588
589
590
591<a name="translatingfn"></a>
592<h2>Translating Field Names</h2>
593
594<p>If you would like to store the "human" name you passed to the <dfn>set_rules()</dfn> function in a language file, and therefore make the name able to be translated, here's how:</p>
595
596<p>First, prefix your "human" name with <dfn>lang:</dfn>, as in this example:</p>
597
598<code>
599$this->form_validation->set_rules('first_name', '<kbd>lang:</kbd>first_name', 'required');<br />
600</code>
601
602<p>Then, store the name in one of your language file arrays (without the prefix):</p>
603
604<code>$lang['first_name'] = 'First Name';</code>
605
606<p><strong>Note:</strong> If you store your array item in a language file that is not loaded automatically by CI, you'll need to remember to load it in your controller using:</p>
607
608<code>$this->lang->load('file_name');</code>
609
610<p>See the <a href="language.html">Language Class</a> page for more info regarding language files.</p>
611
612
613<a name="errordelimiters"></a>
614<h2>Changing the Error Delimiters</h2>
615
Barry Mienydd671972010-10-04 16:33:58 +0200616<p>By default, the Form Validation class adds a paragraph tag (&lt;p&gt;) around each error message shown. You can either change these delimiters globally or
Derek Allard2067d1a2008-11-13 22:59:24 +0000617individually.</p>
618
619<ol>
620
621<li><strong>Changing delimiters Globally</strong>
622
623<p>To globally change the error delimiters, in your controller function, just after loading the Form Validation class, add this:</p>
624
625<code>$this->form_validation->set_error_delimiters('<kbd>&lt;div class="error"></kbd>', '<kbd>&lt;/div></kbd>');</code>
626
627<p>In this example, we've switched to using div tags.</p>
628
629</li>
630
631<li><strong>Changing delimiters Individually</strong>
632
633<p>Each of the two error generating functions shown in this tutorial can be supplied their own delimiters as follows:</p>
634
635<code>&lt;?php echo form_error('field name', '<kbd>&lt;div class="error"></kbd>', '<kbd>&lt;/div></kbd>'); ?></code>
636
637<p>Or:</p>
638
639<code>&lt;?php echo validation_errors('<kbd>&lt;div class="error"></kbd>', '<kbd>&lt;/div></kbd>'); ?></code>
640
641</li>
642</ol>
643
644
645
646
647<a name="individualerrors"></a>
648<h2>Showing Errors Individually</h2>
649
650<p>If you prefer to show an error message next to each form field, rather than as a list, you can use the <dfn>form_error()</dfn> function.</p>
651
652<p>Try it! Change your form so that it looks like this:</p>
653
654<textarea class="textarea" style="width:100%" cols="50" rows="18">
655&lt;h5>Username&lt;/h5>
656&lt;?php echo form_error('username'); ?>
657&lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?>" size="50" />
658
659&lt;h5>Password&lt;/h5>
660&lt;?php echo form_error('password'); ?>
661&lt;input type="text" name="password" value="&lt;?php echo set_value('password'); ?>" size="50" />
662
663&lt;h5>Password Confirm&lt;/h5>
664&lt;?php echo form_error('passconf'); ?>
665&lt;input type="text" name="passconf" value="&lt;?php echo set_value('passconf'); ?>" size="50" />
666
667&lt;h5>Email Address&lt;/h5>
668&lt;?php echo form_error('email'); ?>
669&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?>" size="50" />
670</textarea>
671
672<p>If there are no errors, nothing will be shown. If there is an error, the message will appear.</p>
673
674<p><strong>Important Note:</strong> If you use an array as the name of a form field, you must supply it as an array to the function. Example:</p>
675
676<code>&lt;?php echo form_error('<kbd>options[size]</kbd>'); ?><br />
677&lt;input type="text" name="<kbd>options[size]</kbd>" value="&lt;?php echo set_value("<kbd>options[size]</kbd>"); ?>" size="50" />
678</code>
679
680<p>For more info please see the <a href="#arraysasfields">Using Arrays as Field Names</a> section below.</p>
681
682
683
684
685<p>&nbsp;</p>
686
687
688<a name="savingtoconfig"></a>
689<h1>Saving Sets of Validation Rules to a Config File</h1>
690
691<p>A nice feature of the Form Validation class is that it permits you to store all your validation rules for your entire application in a config file. You
692can organize these rules into "groups". These groups can either be loaded automatically when a matching controller/function is called, or
693you can manually call each set as needed.</p>
694
695<h3>How to save your rules</h3>
696
697<p>To store your validation rules, simply create a file named <kbd>form_validation.php</kbd> in your <dfn>application/config/</dfn> folder.
698In that file you will place an array named <kbd>$config</kbd> with your rules. As shown earlier, the validation array will have this prototype:</p>
699
700<code>
701$config = array(<br />
702&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
703&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'field'&nbsp;&nbsp;&nbsp;=> 'username', <br />
704&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'label'&nbsp;&nbsp;&nbsp;=> 'Username', <br />
705&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'rules'&nbsp;&nbsp;&nbsp;=> 'required'<br />
706&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />
722&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
723</code>
724
725<p><dfn>Your validation rule file will be loaded automatically and used when you call the run() function.</dfn></p>
726
727<p class="important">Please note that you MUST name your array $config.</p>
728
729<h3>Creating Sets of Rules</h3>
730
731<p>In order to organize your rules into "sets" requires that you place them into "sub arrays". Consider the following example, showing two sets of rules.
732We've arbitrarily called these two rules "signup" and "email". You can name your rules anything you want:</p>
733
734
735<code>$config = array(<br />
736&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'<kbd>signup</kbd>' => array(<br />
737&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(<br />
738&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'field' => 'username',<br />
739&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />
779&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
780&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
781</code>
782
783
784<h3>Calling a Specific Rule Group</h3>
785
786<p>In order to call a specific group you will pass its name to the <kbd>run()</kbd> function. For example, to call the <kbd>signup</kbd> rule you will do this:</p>
787
788<code>
789if ($this->form_validation->run('<kbd>signup</kbd>') == FALSE)<br />
790{<br />
791&nbsp;&nbsp;&nbsp;$this->load->view('myform');<br />
792}<br />
793else<br />
794{<br />
795&nbsp;&nbsp;&nbsp;$this->load->view('formsuccess');<br />
796}<br />
797</code>
798
799
800
801<h3>Associating a Controller Function with a Rule Group</h3>
802
803<p>An alternate (and more automatic) method of calling a rule group is to name it according to the controller class/function you intend to use it with. For example, let's say you
804have a controller named <kbd>Member</kbd> and a function named <kbd>signup</kbd>. Here's what your class might look like:</p>
805
806<code>
807&lt;?php<br /><br />
808class <kbd>Member</kbd> extends Controller {<br />
809<br />
810&nbsp;&nbsp;&nbsp;function <kbd>signup</kbd>()<br />
811&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
812&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->library('form_validation');<br />
813&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
814&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($this->form_validation->run() == FALSE)<br />
815&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
816&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('myform');<br />
817&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
818&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br />
819&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />
820&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('formsuccess');<br />
821&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
822&nbsp;&nbsp;&nbsp;}<br />
823}<br />
824?></code>
825
826<p>In your validation config file, you will name your rule group <kbd>member/signup</kbd>:</p>
827
828
829<code>$config = array(<br />
Derek Allard03d783b2009-05-03 19:45:45 +0000830&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'<kbd>member/signup</kbd>' => array(<br />
Derek Allard2067d1a2008-11-13 22:59:24 +0000831&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />
852&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
853</code>
854
855<p><dfn>When a rule group is named identically to a controller class/function it will be used automatically when the run() function is invoked from that class/function.</dfn></p>
856
857<p>&nbsp;</p>
858
859
860<a name="arraysasfields"></a>
861<h1>Using Arrays as Field Names</h1>
862
863<p>The Form Validation class supports the use of arrays as field names. Consider this example:</p>
864
865<code>&lt;input type="text" name="<kbd>options[]</kbd>" value="" size="50" /></code>
866
867<p>If you do use an array as a field name, you must use the EXACT array name in the <a href="#helperreference">Helper Functions</a> that require the field name,
868and as your Validation Rule field name.</p>
869
870<p>For example, to set a rule for the above field you would use:</p>
871
872<code>$this->form_validation->set_rules('<kbd>options[]</kbd>', 'Options', 'required');</code>
873
874<p>Or, to show an error for the above field you would use:</p>
875
876<code>&lt;?php echo form_error('<kbd>options[]</kbd>'); ?></code>
877
878<p>Or to re-populate the field you would use:</p>
879
880<code>&lt;input type="text" name="<kbd>options[]</kbd>" value="<kbd>&lt;?php echo set_value('<kbd>options[]</kbd>'); ?></kbd>" size="50" /></code>
881
882<p>You can use multidimensional arrays as field names as well. For example:</p>
883
884<code>&lt;input type="text" name="<kbd>options[size]</kbd>" value="" size="50" /></code>
885
886<p>Or even:</p>
887
888<code>&lt;input type="text" name="<kbd>sports[nba][basketball]</kbd>" value="" size="50" /></code>
889
890<p>As with our first example, you must use the exact array name in the helper functions:</p>
891
892<code>&lt;?php echo form_error('<kbd>sports[nba][basketball]</kbd>'); ?></code>
893
894<p>If you are using checkboxes (or other fields) that have multiple options, don't forget to leave an empty bracket after each option, so that all selections will be added to the
895POST array:</p>
896
897<code>
898&lt;input type="checkbox" name="<kbd>options[]</kbd>" value="red" /><br />
899&lt;input type="checkbox" name="<kbd>options[]</kbd>" value="blue" /><br />
900&lt;input type="checkbox" name="<kbd>options[]</kbd>" value="green" />
901</code>
902
903<p>Or if you use a multidimensional array:</p>
904
905<code>
906&lt;input type="checkbox" name="<kbd>options[color][]</kbd>" value="red" /><br />
907&lt;input type="checkbox" name="<kbd>options[color][]</kbd>" value="blue" /><br />
908&lt;input type="checkbox" name="<kbd>options[color][]</kbd>" value="green" />
909</code>
910
911<p>When you use a helper function you'll include the bracket as well:</p>
912
913<code>&lt;?php echo form_error('<kbd>options[color][]</kbd>'); ?></code>
914
915
916
917
918<p>&nbsp;</p>
919
920
921<a name="rulereference"></a>
922<h1>Rule Reference</h1>
923
924<p>The following is a list of all the native rules that are available to use:</p>
925
926
927
928<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
929<tr>
930<th>Rule</th>
931<th>Parameter</th>
932<th>Description</th>
933<th>Example</th>
934</tr><tr>
935
936<td class="td"><strong>required</strong></td>
937<td class="td">No</td>
938<td class="td">Returns FALSE if the form element is empty.</td>
939<td class="td">&nbsp;</td>
940</tr><tr>
941
942<td class="td"><strong>matches</strong></td>
943<td class="td">Yes</td>
944<td class="td">Returns FALSE if the form element does not match the one in the parameter.</td>
945<td class="td">matches[form_item]</td>
946</tr><tr>
947
948<td class="td"><strong>min_length</strong></td>
949<td class="td">Yes</td>
950<td class="td">Returns FALSE if the form element is shorter then the parameter value.</td>
951<td class="td">min_length[6]</td>
952</tr><tr>
953
954<td class="td"><strong>max_length</strong></td>
955<td class="td">Yes</td>
956<td class="td">Returns FALSE if the form element is longer then the parameter value.</td>
957<td class="td">max_length[12]</td>
958</tr><tr>
959
960<td class="td"><strong>exact_length</strong></td>
961<td class="td">Yes</td>
962<td class="td">Returns FALSE if the form element is not exactly the parameter value.</td>
963<td class="td">exact_length[8]</td>
964</tr><tr>
965
966<td class="td"><strong>alpha</strong></td>
967<td class="td">No</td>
968<td class="td">Returns FALSE if the form element contains anything other than alphabetical characters.</td>
969<td class="td">&nbsp;</td>
970</tr><tr>
971
972<td class="td"><strong>alpha_numeric</strong></td>
973<td class="td">No</td>
974<td class="td">Returns FALSE if the form element contains anything other than alpha-numeric characters.</td>
975<td class="td">&nbsp;</td>
976</tr><tr>
977
978<td class="td"><strong>alpha_dash</strong></td>
979<td class="td">No</td>
980<td class="td">Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.</td>
981<td class="td">&nbsp;</td>
982</tr>
983
984<tr>
985<td class="td"><strong>numeric</strong></td>
986<td class="td">No</td>
987<td class="td">Returns FALSE if the form element contains anything other than numeric characters.</td>
988<td class="td">&nbsp;</td>
989</tr>
990
991<tr>
992<td class="td"><strong>integer</strong></td>
993<td class="td">No</td>
994<td class="td">Returns FALSE if the form element contains anything other than an integer.</td>
995<td class="td">&nbsp;</td>
996</tr>
997
998<tr>
999<td class="td"><strong>is_natural</strong></td>
1000<td class="td">No</td>
1001<td class="td">Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc.</td>
1002<td class="td">&nbsp;</td>
1003</tr>
1004
1005<tr>
1006<td class="td"><strong>is_natural_no_zero</strong></td>
1007<td class="td">No</td>
1008<td class="td">Returns FALSE if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc.</td>
1009<td class="td">&nbsp;</td>
1010</tr>
1011
1012<tr>
1013<td class="td"><strong>valid_email</strong></td>
1014<td class="td">No</td>
1015<td class="td">Returns FALSE if the form element does not contain a valid email address.</td>
1016<td class="td">&nbsp;</td>
1017</tr>
1018
1019<tr>
1020<td class="td"><strong>valid_emails</strong></td>
1021<td class="td">No</td>
1022<td class="td">Returns FALSE if any value provided in a comma separated list is not a valid email.</td>
1023<td class="td">&nbsp;</td>
1024</tr>
1025
1026<tr>
1027<td class="td"><strong>valid_ip</strong></td>
1028<td class="td">No</td>
1029<td class="td">Returns FALSE if the supplied IP is not valid.</td>
1030<td class="td">&nbsp;</td>
1031</tr>
1032
1033<tr>
1034<td class="td"><strong>valid_base64</strong></td>
1035<td class="td">No</td>
1036<td class="td">Returns FALSE if the supplied string contains anything other than valid Base64 characters.</td>
1037<td class="td">&nbsp;</td>
1038</tr>
1039
1040
1041</table>
1042
1043<p><strong>Note:</strong> These rules can also be called as discrete functions. For example:</p>
1044
1045<code>$this->form_validation->required($string);</code>
1046
1047<p class="important"><strong>Note:</strong> You can also use any native PHP functions that permit one parameter.</p>
1048
1049
1050
1051<p>&nbsp;</p>
1052
1053<a name="preppingreference"></a>
1054<h1>Prepping Reference</h1>
1055
1056<p>The following is a list of all the prepping functions that are available to use:</p>
1057
1058
1059
1060<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
1061<tr>
1062<th>Name</th>
1063<th>Parameter</th>
1064<th>Description</th>
1065</tr><tr>
1066
1067<td class="td"><strong>xss_clean</strong></td>
1068<td class="td">No</td>
1069<td class="td">Runs the data through the XSS filtering function, described in the <a href="input.html">Input Class</a> page.</td>
1070</tr><tr>
1071
1072<td class="td"><strong>prep_for_form</strong></td>
1073<td class="td">No</td>
1074<td class="td">Converts special characters so that HTML data can be shown in a form field without breaking it.</td>
1075</tr><tr>
1076
1077<td class="td"><strong>prep_url</strong></td>
1078<td class="td">No</td>
1079<td class="td">Adds "http://" to URLs if missing.</td>
1080</tr><tr>
1081
1082<td class="td"><strong>strip_image_tags</strong></td>
1083<td class="td">No</td>
1084<td class="td">Strips the HTML from image tags leaving the raw URL.</td>
1085</tr><tr>
1086
1087<td class="td"><strong>encode_php_tags</strong></td>
1088<td class="td">No</td>
1089<td class="td">Converts PHP tags to entities.</td>
1090</tr>
1091
1092</table>
1093
1094<p class="important"><strong>Note:</strong> You can also use any native PHP functions that permit one parameter,
1095like <kbd>trim</kbd>, <kbd>htmlspecialchars</kbd>, <kbd>urldecode</kbd>, etc.</p>
1096
1097
1098
1099
1100
1101
1102
1103<p>&nbsp;</p>
1104
1105<a name="functionreference"></a>
1106<h1>Function Reference</h1>
1107
1108<p>The following functions are intended for use in your controller functions.</p>
1109
Derek Allard51157252009-02-04 12:34:07 +00001110<h2>$this->form_validation->set_rules();</h2>
Derek Allard2067d1a2008-11-13 22:59:24 +00001111
1112<p>Permits you to set validation rules, as described in the tutorial sections above:</p>
1113
1114<ul>
1115<li><a href="#validationrules">Setting Validation Rules</a></li>
1116<li><a href="#savingtoconfig">Saving Groups of Validation Rules to a Config File</a></li>
1117</ul>
1118
1119
1120<h2>$this->form_validation->run();</h2>
1121
1122<p>Runs the validation routines. Returns boolean TRUE on success and FALSE on failure. You can optionally pass the name of the validation
1123group via the function, as described in: <a href="#savingtoconfig">Saving Groups of Validation Rules to a Config File</a>.</p>
1124
1125
1126<h2>$this->form_validation->set_message();</h2>
1127
1128<p>Permits you to set custom error messages. See <a href="#settingerrors">Setting Error Messages</a> above.</p>
1129
1130
1131<p>&nbsp;</p>
1132
1133<a name="helperreference"></a>
1134<h1>Helper Reference</h1>
1135
1136<p>The following helper functions are available for use in the view files containing your forms. Note that these are procedural functions, so they
1137<strong>do not</strong> require you to prepend them with $this->form_validation.</p>
1138
1139<h2>form_error()</h2>
1140
1141<p>Shows an individual error message associated with the field name supplied to the function. Example:</p>
1142
1143<code>&lt;?php echo form_error('username'); ?></code>
1144
1145<p>The error delimiters can be optionally specified. See the <a href="#errordelimiters">Changing the Error Delimiters</a> section above.</p>
1146
1147
1148
1149<h2>validation_errors()</h2>
1150<p>Shows all error messages as a string: Example:</p>
1151
1152<code>&lt;?php echo validation_errors(); ?></code>
1153
1154<p>The error delimiters can be optionally specified. See the <a href="#errordelimiters">Changing the Error Delimiters</a> section above.</p>
1155
1156
1157
1158<h2>set_value()</h2>
1159
1160<p>Permits you to set the value of an input form or textarea. You must supply the field name via the first parameter of the function.
1161The second (optional) parameter allows you to set a default value for the form. Example:</p>
1162
1163<code>&lt;input type="text" name="quantity" value="<dfn>&lt;?php echo set_value('quantity', '0'); ?></dfn>" size="50" /></code>
1164
1165<p>The above form will show "0" when loaded for the first time.</p>
1166
1167<h2>set_select()</h2>
1168
1169<p>If you use a <dfn>&lt;select></dfn> menu, this function permits you to display the menu item that was selected. The first parameter
1170must contain the name of the select menu, the second parameter must contain the value of
1171each item, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE).</p>
1172
1173<p>Example:</p>
1174
1175<code>
1176&lt;select name="myselect"><br />
1177&lt;option value="one" <dfn>&lt;?php echo set_select('myselect', 'one', TRUE); ?></dfn> >One&lt;/option><br />
1178&lt;option value="two" <dfn>&lt;?php echo set_select('myselect', 'two'); ?></dfn> >Two&lt;/option><br />
1179&lt;option value="three" <dfn>&lt;?php echo set_select('myselect', 'three'); ?></dfn> >Three&lt;/option><br />
1180&lt;/select>
1181</code>
1182
1183
1184<h2>set_checkbox()</h2>
1185
1186<p>Permits you to display a checkbox in the state it was submitted. The first parameter
1187must contain the name of the checkbox, the second parameter must contain its value, and the third (optional) parameter lets you set an item as the default (use boolean TRUE/FALSE). Example:</p>
1188
1189<code>&lt;input type="checkbox" name="mycheck[]" value="1" <dfn>&lt;?php echo set_checkbox('mycheck[]', '1'); ?></dfn> /><br />
1190&lt;input type="checkbox" name="mycheck[]" value="2" <dfn>&lt;?php echo set_checkbox('mycheck[]', '2'); ?></dfn> /></code>
1191
1192
1193<h2>set_radio()</h2>
1194
1195<p>Permits you to display radio buttons in the state they were submitted. This function is identical to the <strong>set_checkbox()</strong> function above.</p>
1196
1197<code>&lt;input type="radio" name="myradio" value="1" <dfn>&lt;?php echo set_radio('myradio', '1', TRUE); ?></dfn> /><br />
1198&lt;input type="radio" name="myradio" value="2" <dfn>&lt;?php echo set_radio('myradio', '2'); ?></dfn> /></code>
1199
1200
1201
1202</div>
1203<!-- END CONTENT -->
1204
1205
1206<div id="footer">
1207<p>
1208Previous Topic:&nbsp;&nbsp;<a href="file_uploading.html">File Uploading Class</a>
1209&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
1210<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
1211<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
1212Next Topic:&nbsp;&nbsp;<a href="ftp.html">FTP Class</a>
1213</p>
Derek Jonesd6d70e32010-03-29 10:10:27 -05001214<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2010 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +00001215</div>
1216
1217</body>
Rick Ellis25949532008-08-26 19:48:08 +00001218</html>