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