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