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