blob: a2d6287b0739c59e3bde3e0a7df12dba3d5c3111 [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 Jonesbbedc762009-09-11 14:47:37 +000030<td><h1>CodeIgniter User Guide Version 1.7.2</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
Derek Allardd13f0122009-04-14 19:45:51 +000057<p class="important">
58 This library has been deprecated. Use of the form_validation library is encouraged.
59</p>
60
Derek Allard2067d1a2008-11-13 22:59:24 +000061<h1>Form Validation</h1>
62
63<p>Before explaining CodeIgniter's approach to data validation, let's describe the ideal scenario:</p>
64
65<ol>
66<li>A form is displayed.</li>
67<li>You fill it in and submit it.</li>
68<li>If you submitted something invalid, or perhaps missed a required item, the form is redisplayed containing your data along with an error message describing the problem.</li>
69<li>This process continues until you have submitted a valid form.</li>
70</ol>
71
72<p>On the receiving end, the script must:</p>
73
74<ol>
75<li>Check for required data.</li>
76<li>Verify that the data is of the correct type, and meets the correct criteria. (For example, if a username is submitted
77it must be validated to contain only permitted characters. It must be of a minimum length,
78and not exceed a maximum length. The username can't be someone else's existing username, or perhaps even a reserved word. Etc.)</li>
79<li>Sanitize the data for security.</li>
80<li>Pre-format the data if needed (Does the data need to be trimmed? HTML encoded? Etc.)</li>
81<li>Prep the data for insertion in the database.</li>
82</ol>
83
84
85<p>Although there is nothing complex about the above process, it usually requires a significant
86amount of code, and to display error messages, various control structures are usually placed within the form HTML.
87Form validation, while simple to create, is generally very messy and tedious to implement.</p>
88
89<dfn>CodeIgniter provides a comprehensive validation framework that truly minimizes the amount of code you'll write.
90It also removes all control structures from your form HTML, permitting it to be clean and free of code.</dfn>
91
92<h2>Overview</h2>
93
94<p>In order to implement CodeIgniter's form validation you'll need three things:</p>
95
96<ol>
97<li>A <a href="../general/views.html">View</a> file containing the form.</li>
98<li>A View file containing a "success" message to be displayed upon successful submission.</li>
99<li>A <a href="../general/controllers.html">controller</a> function to receive and process the submitted data.</li>
100</ol>
101
102<p>Let's create those three things, using a member sign-up form as the example.</p>
103
104<h2>The Form</h2>
105
106<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>
107folder:</p>
108
109
110<textarea class="textarea" style="width:100%" cols="50" rows="30">&lt;html>
111&lt;head>
112&lt;title>My Form&lt;/title>
113&lt;/head>
114&lt;body>
115
116&lt;?php echo $this->validation->error_string; ?>
117
118&lt;?php echo form_open('form'); ?>
119
120&lt;h5>Username&lt;/h5>
121&lt;input type="text" name="username" value="" size="50" />
122
123&lt;h5>Password&lt;/h5>
124&lt;input type="text" name="password" value="" size="50" />
125
126&lt;h5>Password Confirm&lt;/h5>
127&lt;input type="text" name="passconf" value="" size="50" />
128
129&lt;h5>Email Address&lt;/h5>
130&lt;input type="text" name="email" value="" size="50" />
131
132&lt;div>&lt;input type="submit" value="Submit" />&lt;/div>
133
134&lt;/form>
135
136&lt;/body>
137&lt;/html>
138</textarea>
139
140
141<h2>The Success Page</h2>
142
143
144<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>
145folder:</p>
146
147
148<textarea class="textarea" style="width:100%" cols="50" rows="14">
149&lt;html>
150&lt;head>
151&lt;title>My Form&lt;/title>
152&lt;/head>
153&lt;body>
154
155&lt;h3>Your form was successfully submitted!&lt;/h3>
156
157&lt;p>&lt;?php echo anchor('form', 'Try it again!'); ?>&lt;/p>
158
159&lt;/body>
160&lt;/html>
161</textarea>
162
163
164<h2>The Controller</h2>
165
166<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>
167folder:</p>
168
169
170<textarea class="textarea" style="width:100%" cols="50" rows="21">&lt;?php
171
172class Form extends Controller {
173
174 function index()
175 {
176 $this->load->helper(array('form', 'url'));
177
178 $this->load->library('validation');
179
180 if ($this->validation->run() == FALSE)
181 {
182 $this->load->view('myform');
183 }
184 else
185 {
186 $this->load->view('formsuccess');
187 }
188 }
189}
190?></textarea>
191
192
193<h2>Try it!</h2>
194
195<p>To try your form, visit your site using a URL similar to this one:</p>
196
197<code>example.com/index.php/<var>form</var>/</code>
198
199<p><strong>If you submit the form you should simply see the form reload. That's because you haven't set up any validation
200rules yet, which we'll get to in a moment.</strong></p>
201
202
203<h2>Explanation</h2>
204
205<p>You'll notice several things about the above pages:</p>
206
207<p>The <dfn>form</dfn> (myform.php) is a standard web form with a couple exceptions:</p>
208
209<ol>
210<li>It uses a <dfn>form helper</dfn> to create the form opening.
211Technically, this isn't necessary. You could create the form using standard HTML. However, the benefit of using the helper
212is that it generates the action URL for you, based on the URL in your config file. This makes your application more portable
213and flexible in the event your URLs change.</li>
214
215<li>At the top of the form you'll notice the following variable:
216<code>&lt;?php echo $this->validation->error_string; ?&gt;</code>
217
218<p>This variable will display any error messages sent back by the validator. If there are no messages it returns nothing.</p>
219</li>
220</ol>
221
222<p>The <dfn>controller</dfn> (form.php) has one function: <dfn>index()</dfn>. This function initializes the validation class and
223loads the <var>form helper</var> and <var>URL helper</var> used by your view files. It also <samp>runs</samp>
224the validation routine. Based on
225whether the validation was successful it either presents the form or the success page.</p>
226
227<p><strong>Since you haven't told the validation class to validate anything yet, it returns "false" (boolean false) by default. The <samp>run()</samp>
228function only returns "true" if it has successfully applied your rules without any of them failing.</strong></p>
229
230
231<h2>Setting Validation Rules</h2>
232
233<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
234at the same time. Let's see it in action, we'll explain it afterwards.</p>
235
236<p>In your <dfn>controller</dfn> (form.php), add this code just below the validation initialization function:</p>
237
238<code>$rules['username'] = "required";<br />
239$rules['password'] = "required";<br />
240$rules['passconf'] = "required";<br />
241$rules['email'] = "required";<br />
242<br />
243$this->validation->set_rules($rules);</code>
244
245<p>Your controller should now look like this:</p>
246
Derek Allard4b6d4932008-12-07 17:04:56 +0000247<textarea class="textarea" style="width:100%" cols="50" rows="28">&lt;?php
Derek Allard2067d1a2008-11-13 22:59:24 +0000248
249class Form extends Controller {
250
251 function index()
252 {
253 $this->load->helper(array('form', 'url'));
254
255 $this->load->library('validation');
256
257 $rules['username'] = "required";
258 $rules['password'] = "required";
259 $rules['passconf'] = "required";
260 $rules['email'] = "required";
261
262 $this->validation->set_rules($rules);
263
264 if ($this->validation->run() == FALSE)
265 {
266 $this->load->view('myform');
267 }
268 else
269 {
270 $this->load->view('formsuccess');
271 }
272 }
273}
274?></textarea>
275
276<p><dfn>Now submit the form with the fields blank and you should see the error message.
277If you submit the form with all the fields populated you'll see your success page.</dfn></p>
278
279<p class="important"><strong>Note:</strong> The form fields are not yet being re-populated with the data when
280there is an error. We'll get to that shortly, once we're through explaining the validation rules.</p>
281
282
283<h2>Changing the Error Delimiters</h2>
284
285<p>By default, the system adds a paragraph tag (&lt;p&gt;) around each error message shown. You can easily change these delimiters with
286this code, placed in your controller:</p>
287
288<code>$this->validation->set_error_delimiters('<kbd>&lt;div class="error"></kbd>', '<kbd>&lt;/div></kbd>');</code>
289
290<p>In this example, we've switched to using div tags.</p>
291
292<h2>Cascading Rules</h2>
293
294<p>CodeIgniter lets you pipe multiple rules together. Let's try it. Change your rules array like this:</p>
295
296
297<code>$rules['username'] = "required|min_length[5]|max_length[12]";<br />
298$rules['password'] = "required|matches[passconf]";<br />
299$rules['passconf'] = "required";<br />
300$rules['email'] = "required|valid_email";</code>
301
302<p>The above code requires that:</p>
303
304<ol>
305<li>The username field be no shorter than 5 characters and no longer than 12.</li>
306<li>The password field must match the password confirmation field.</li>
307<li>The email field must contain a valid email address.</li>
308</ol>
309
310<p>Give it a try!</p>
311
312<p class="important"><strong>Note:</strong> There are numerous rules available which you can read about in the validation reference.</p>
313
314
315<h2>Prepping Data</h2>
316
317<p>In addition to the validation functions like the ones we used above, you can also prep your data in various ways.
318For example, you can set up rules like this:</p>
319
320<code>$rules['username'] = "<kbd>trim</kbd>|required|min_length[5]|max_length[12]|<kbd>xss_clean</kbd>";<br />
321$rules['password'] = "<kbd>trim</kbd>|required|matches[passconf]|<kbd>md5</kbd>";<br />
322$rules['passconf'] = "<kbd>trim</kbd>|required";<br />
323$rules['email'] = "<kbd>trim</kbd>|required|valid_email";</code>
324
325<p>In the above example, we are "trimming" the fields, converting the password to MD5, and running the username through
326the "xss_clean" function, which removes malicious data.</p>
327
328<p class="important"><strong>Any native PHP function that accepts one parameter can be used as a rule, like <dfn>htmlspecialchars</dfn>,
329<dfn>trim</dfn>, <dfn>MD5</dfn>, etc.</strong></p>
330
331<p><strong>Note:</strong> You will generally want to use the prepping functions <strong>after</strong>
332the validation rules so if there is an error, the original data will be shown in the form.</p>
333
334<h2>Callbacks: Your own Validation Functions</h2>
335
336<p>The validation system supports callbacks to your own validation functions. This permits you to extend the validation class
337to 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
338create a callback function that does that. Let's create a simple example.</p>
339
340<p>In your controller, change the "username" rule to this:</p>
341
342<code>$rules['username'] = "callback_username_check"; </code>
343
344<p>Then add a new function called <dfn>username_check</dfn> to your controller. Here's how your controller should look:</p>
345
346
Derek Allard4b6d4932008-12-07 17:04:56 +0000347<textarea class="textarea" style="width:100%" cols="50" rows="44">&lt;?php
Derek Allard2067d1a2008-11-13 22:59:24 +0000348
349class Form extends Controller {
350
351 function index()
352 {
353 $this->load->helper(array('form', 'url'));
354
355 $this->load->library('validation');
356
357 $rules['username'] = "callback_username_check";
358 $rules['password'] = "required";
359 $rules['passconf'] = "required";
360 $rules['email'] = "required";
361
362 $this->validation->set_rules($rules);
363
364 if ($this->validation->run() == FALSE)
365 {
366 $this->load->view('myform');
367 }
368 else
369 {
370 $this->load->view('formsuccess');
371 }
372 }
373
374 function username_check($str)
375 {
376 if ($str == 'test')
377 {
378 $this->validation->set_message('username_check', 'The %s field can not be the word "test"');
379 return FALSE;
380 }
381 else
382 {
383 return TRUE;
384 }
385 }
386
387}
388?></textarea>
389
390<p>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
391callback function for you to process.</p>
392
393<p><strong>To invoke a callback just put the function name in a rule, with "callback_" as the rule prefix.</strong></p>
394
395<p>The error message was set using the <dfn>$this->validation->set_message</dfn> function.
396Just remember that the message key (the first parameter) must match your function name.</p>
397
398<p class="important"><strong>Note:</strong> You can apply your own custom error messages to any rule, just by setting the
399message similarly. For example, to change the message for the "required" rule you will do this:</p>
400
401<code>$this->validation->set_message('required', 'Your custom message here');</code>
402
403<h2>Re-populating the form</h2>
404
405<p>Thus far we have only been dealing with errors. It's time to repopulate the form field with the submitted data.
406This is done similarly to your rules. Add the following code to your controller, just below your rules:</p>
407
408<code>$fields['username'] = 'Username';<br />
409$fields['password'] = 'Password';<br />
410$fields['passconf'] = 'Password Confirmation';<br />
411$fields['email'] = 'Email Address';<br />
412<br />
413$this->validation->set_fields($fields);</code>
414
415<p>The array keys are the actual names of the form fields, the value represents the full name that you want shown in the
416error message.</p>
417
418<p>The index function of your controller should now look like this:</p>
419
420
421<textarea class="textarea" style="width:100%" cols="50" rows="30">function index()
422{
423 $this->load->helper(array('form', 'url'));
424
425 $this->load->library('validation');
426
427 $rules['username'] = "required";
428 $rules['password'] = "required";
429 $rules['passconf'] = "required";
430 $rules['email'] = "required";
431
432 $this->validation->set_rules($rules);
433
434 $fields['username'] = 'Username';
435 $fields['password'] = 'Password';
436 $fields['passconf'] = 'Password Confirmation';
437 $fields['email'] = 'Email Address';
438
439 $this->validation->set_fields($fields);
440
441 if ($this->validation->run() == FALSE)
442 {
443 $this->load->view('myform');
444 }
445 else
446 {
447 $this->load->view('formsuccess');
448 }
449}</textarea>
450
451
452<p>Now open your <dfn>myform.php</dfn> view file and update the value in each field so that it has an attribute corresponding to its name:</p>
453
454
455<textarea class="textarea" style="width:100%" cols="50" rows="30">
456&lt;html>
457&lt;head>
458&lt;title>My Form&lt;/title>
459&lt;/head>
460&lt;body>
461
462&lt;?php echo $this->validation->error_string; ?>
463
464&lt;?php echo form_open('form'); ?>
465
466&lt;h5>Username&lt;/h5>
467&lt;input type="text" name="username" value="&lt;?php echo $this->validation->username;?>" size="50" />
468
469&lt;h5>Password&lt;/h5>
470&lt;input type="text" name="password" value="&lt;?php echo $this->validation->password;?>" size="50" />
471
472&lt;h5>Password Confirm&lt;/h5>
473&lt;input type="text" name="passconf" value="&lt;?php echo $this->validation->passconf;?>" size="50" />
474
475&lt;h5>Email Address&lt;/h5>
476&lt;input type="text" name="email" value="&lt;?php echo $this->validation->email;?>" size="50" />
477
478&lt;div>&lt;input type="submit" value="Submit" />&lt;/div>
479
480&lt;/form>
481
482&lt;/body>
483&lt;/html>
484</textarea>
485
486
487<p>Now reload your page and submit the form so that it triggers an error. Your form fields should be populated
488and the error messages will contain a more relevant field name.</p>
489
490
491
492<h2>Showing Errors Individually</h2>
493
494<p>If you prefer to show an error message next to each form field, rather than as a list, you can change your form so that it looks like this:</p>
495
496
497<textarea class="textarea" style="width:100%" cols="50" rows="20">
498&lt;h5>Username&lt;/h5>
499&lt;?php echo $this->validation->username_error; ?>
500&lt;input type="text" name="username" value="&lt;?php echo $this->validation->username;?>" size="50" />
501
502&lt;h5>Password&lt;/h5>
503&lt;?php echo $this->validation->password_error; ?>
504&lt;input type="text" name="password" value="&lt;?php echo $this->validation->password;?>" size="50" />
505
506&lt;h5>Password Confirm&lt;/h5>
507&lt;?php echo $this->validation->passconf_error; ?>
508&lt;input type="text" name="passconf" value="&lt;?php echo $this->validation->passconf;?>" size="50" />
509
510&lt;h5>Email Address&lt;/h5>
511&lt;?php echo $this->validation->email_error; ?>
512&lt;input type="text" name="email" value="&lt;?php echo $this->validation->email;?>" size="50" /></textarea>
513
514<p>If there are no errors, nothing will be shown. If there is an error, the message will appear, wrapped in the delimiters you
515have set (&lt;p> tags by default).</p>
516
517<p class="important"><strong>Note: </strong>To display errors this way you must remember to set your fields using the <kbd>$this->validation->set_fields</kbd>
518function described earlier. The errors will be turned into variables that have "_error" after your field name.
519For example, your "username" error will be available at:<br /><dfn>$this->validation->username_error</dfn>.</p>
520
521
522<h2>Rule Reference</h2>
523
524<p>The following is a list of all the native rules that are available to use:</p>
525
526
527
528<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
529<tr>
530<th>Rule</th>
531<th>Parameter</th>
532<th>Description</th>
533<th>Example</th>
534</tr><tr>
535
536<td class="td"><strong>required</strong></td>
537<td class="td">No</td>
538<td class="td">Returns FALSE if the form element is empty.</td>
539<td class="td">&nbsp;</td>
540</tr><tr>
541
542<td class="td"><strong>matches</strong></td>
543<td class="td">Yes</td>
544<td class="td">Returns FALSE if the form element does not match the one in the parameter.</td>
545<td class="td">matches[form_item]</td>
546</tr><tr>
547
548<td class="td"><strong>min_length</strong></td>
549<td class="td">Yes</td>
550<td class="td">Returns FALSE if the form element is shorter then the parameter value.</td>
551<td class="td">min_length[6]</td>
552</tr><tr>
553
554<td class="td"><strong>max_length</strong></td>
555<td class="td">Yes</td>
556<td class="td">Returns FALSE if the form element is longer then the parameter value.</td>
557<td class="td">max_length[12]</td>
558</tr><tr>
559
560<td class="td"><strong>exact_length</strong></td>
561<td class="td">Yes</td>
562<td class="td">Returns FALSE if the form element is not exactly the parameter value.</td>
563<td class="td">exact_length[8]</td>
564</tr><tr>
565
566<td class="td"><strong>alpha</strong></td>
567<td class="td">No</td>
568<td class="td">Returns FALSE if the form element contains anything other than alphabetical characters.</td>
569<td class="td">&nbsp;</td>
570</tr><tr>
571
572<td class="td"><strong>alpha_numeric</strong></td>
573<td class="td">No</td>
574<td class="td">Returns FALSE if the form element contains anything other than alpha-numeric characters.</td>
575<td class="td">&nbsp;</td>
576</tr><tr>
577
578<td class="td"><strong>alpha_dash</strong></td>
579<td class="td">No</td>
580<td class="td">Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.</td>
581<td class="td">&nbsp;</td>
582</tr>
583<tr>
584 <td class="td"><strong>numeric</strong></td>
585 <td class="td">No</td>
586 <td class="td">Returns FALSE if the form element contains anything other than numeric characters.</td>
587 <td class="td">&nbsp;</td>
588</tr>
589<tr>
590
591<td class="td"><strong>integer</strong></td>
592<td class="td">No</td>
593<td class="td">Returns FALSE if the form element contains anything other than an integer.</td>
594<td class="td">&nbsp;</td>
595</tr><tr>
596
597<td class="td"><strong>valid_email</strong></td>
598<td class="td">No</td>
599<td class="td">Returns FALSE if the form element does not contain a valid email address.</td>
600<td class="td">&nbsp;</td>
601</tr>
602<tr>
603 <td class="td"><strong>valid_emails</strong></td>
604 <td class="td">No</td>
605 <td class="td">Returns FALSE if any value provided in a comma separated list is not a valid email.</td>
606 <td class="td">&nbsp;</td>
607</tr>
608<tr>
609<td class="td"><strong>valid_ip</strong></td>
610<td class="td">No</td>
611<td class="td">Returns FALSE if the supplied IP is not valid.</td>
612<td class="td">&nbsp;</td>
613</tr>
614<tr>
615 <td class="td"><strong>valid_base64</strong></td>
616 <td class="td">No</td>
617 <td class="td">Returns FALSE if the supplied string contains anything other than valid Base64 characters.</td>
618 <td class="td">&nbsp;</td>
619</tr>
620</table>
621
622<p><strong>Note:</strong> These rules can also be called as discrete functions. For example:</p>
623
624<code>$this->validation->required($string);</code>
625
626<p class="important"><strong>Note:</strong> You can also use any native PHP functions that permit one parameter.</p>
627
628
629
630<h2>Prepping Reference</h2>
631
632<p>The following is a list of all the prepping functions that are available to use:</p>
633
634
635
636<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
637<tr>
638<th>Name</th>
639<th>Parameter</th>
640<th>Description</th>
641</tr><tr>
642
643<td class="td"><strong>xss_clean</strong></td>
644<td class="td">No</td>
645<td class="td">Runs the data through the XSS filtering function, described in the <a href="input.html">Input Class</a> page.</td>
646</tr><tr>
647
648<td class="td"><strong>prep_for_form</strong></td>
649<td class="td">No</td>
650<td class="td">Converts special characters so that HTML data can be shown in a form field without breaking it.</td>
651</tr><tr>
652
653<td class="td"><strong>prep_url</strong></td>
654<td class="td">No</td>
655<td class="td">Adds "http://" to URLs if missing.</td>
656</tr><tr>
657
658<td class="td"><strong>strip_image_tags</strong></td>
659<td class="td">No</td>
660<td class="td">Strips the HTML from image tags leaving the raw URL.</td>
661</tr><tr>
662
663<td class="td"><strong>encode_php_tags</strong></td>
664<td class="td">No</td>
665<td class="td">Converts PHP tags to entities.</td>
666</tr>
667
668</table>
669
670<p class="important"><strong>Note:</strong> You can also use any native PHP functions that permit one parameter,
671like <kbd>trim</kbd>, <kbd>htmlspecialchars</kbd>, <kbd>urldecode</kbd>, etc.</p>
672
673
674<h2>Setting Custom Error Messages</h2>
675
676<p>All of the native error messages are located in the following language file: <dfn>language/english/validation_lang.php</dfn></p>
677
678<p>To set your own custom message you can either edit that file, or use the following function:</p>
679
680<code>$this->validation->set_message('<var>rule</var>', '<var>Error Message</var>');</code>
681
682<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>
683
684
685<h2>Dealing with Select Menus, Radio Buttons, and Checkboxes</h2>
686
687<p>If you use select menus, radio buttons or checkboxes, you will want the state of
688these items to be retained in the event of an error. The Validation class has three functions that help you do this:</p>
689
690<h2>set_select()</h2>
691
692<p>Permits you to display the menu item that was selected. The first parameter
693must contain the name of the select menu, the second parameter must contain the value of
694each item. Example:</p>
695
696<code>
697&lt;select name="myselect"><br />
698&lt;option value="one" <dfn>&lt;?php echo $this->validation->set_select('myselect', 'one'); ?></dfn> >One&lt;/option><br />
699&lt;option value="two" <dfn>&lt;?php echo $this->validation->set_select('myselect', 'two'); ?></dfn> >Two&lt;/option><br />
700&lt;option value="three" <dfn>&lt;?php echo $this->validation->set_select('myselect', 'three'); ?></dfn> >Three&lt;/option><br />
701&lt;/select>
702</code>
703
704
705<h2>set_checkbox()</h2>
706
707<p>Permits you to display a checkbox in the state it was submitted. The first parameter
708must contain the name of the checkbox, the second parameter must contain its value. Example:</p>
709
710<code>&lt;input type="checkbox" name="mycheck" value="1" <dfn>&lt;?php echo $this->validation->set_checkbox('mycheck', '1'); ?></dfn> /></code>
711
712
713<h2>set_radio()</h2>
714
715<p>Permits you to display radio buttons in the state they were submitted. The first parameter
716must contain the name of the radio button, the second parameter must contain its value. Example:</p>
717
718<code>&lt;input type="radio" name="myradio" value="1" <dfn>&lt;?php echo $this->validation->set_radio('myradio', '1'); ?></dfn> /></code>
719
720
721
722
723
724</div>
725<!-- END CONTENT -->
726
727
728<div id="footer">
729<p>
730Previous Topic:&nbsp;&nbsp;<a href="user_agent.html">User Agent Class</a>
731&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
732<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
733<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
734Next Topic:&nbsp;&nbsp;<a href="xmlrpc.html">XML-RPC Class</a>
735</p>
Derek Jonesfc395a12009-04-22 14:15:09 +0000736<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2009 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
Derek Allard2067d1a2008-11-13 22:59:24 +0000737</div>
738
739</body>
adminb0dd10f2006-08-25 17:25:49 +0000740</html>