Andrey Andreev | c576995 | 2019-01-16 17:49:35 +0200 | [diff] [blame] | 1 | |
| 2 | |
| 3 | <!DOCTYPE html> |
| 4 | <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> |
| 5 | <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> |
| 6 | <head> |
| 7 | <meta charset="utf-8"> |
| 8 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 9 | |
| 10 | <title>Form Validation — CodeIgniter 3.1.10 documentation</title> |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | <link rel="shortcut icon" href="../_static/ci-icon.ico"/> |
| 16 | |
| 17 | |
| 18 | |
| 19 | <link href='https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Roboto+Slab:400,700|Inconsolata:400,700&subset=latin,cyrillic' rel='stylesheet' type='text/css'> |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | <link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" /> |
| 30 | |
| 31 | |
| 32 | |
| 33 | <link rel="index" title="Index" |
| 34 | href="../genindex.html"/> |
| 35 | <link rel="search" title="Search" href="../search.html"/> |
| 36 | <link rel="top" title="CodeIgniter 3.1.10 documentation" href="../index.html"/> |
| 37 | <link rel="up" title="Libraries" href="index.html"/> |
| 38 | <link rel="next" title="FTP Class" href="ftp.html"/> |
| 39 | <link rel="prev" title="File Uploading Class" href="file_uploading.html"/> |
| 40 | |
| 41 | |
| 42 | <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script> |
| 43 | |
| 44 | </head> |
| 45 | |
| 46 | <body class="wy-body-for-nav" role="document"> |
| 47 | |
| 48 | <div id="nav"> |
| 49 | <div id="nav_inner"> |
| 50 | |
| 51 | |
| 52 | |
| 53 | <div id="pulldown-menu" class="ciNav"> |
| 54 | <ul> |
| 55 | <li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">Welcome to CodeIgniter</a></li> |
| 56 | </ul> |
| 57 | <ul> |
| 58 | <li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul> |
| 59 | <li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li> |
| 60 | <li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</a></li> |
| 61 | <li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li> |
| 62 | <li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li> |
| 63 | </ul> |
| 64 | </li> |
| 65 | </ul> |
| 66 | <ul> |
| 67 | <li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul> |
| 68 | <li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li> |
| 69 | <li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li> |
| 70 | <li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li> |
| 71 | <li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li> |
| 72 | <li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li> |
| 73 | <li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li> |
| 74 | </ul> |
| 75 | </li> |
| 76 | </ul> |
| 77 | <ul> |
| 78 | <li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a><ul> |
| 79 | <li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li> |
| 80 | <li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li> |
| 81 | <li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li> |
| 82 | <li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li> |
| 83 | </ul> |
| 84 | </li> |
| 85 | </ul> |
| 86 | <ul> |
| 87 | <li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing to CodeIgniter</a><ul> |
| 88 | <li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li> |
| 89 | <li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</a></li> |
| 90 | </ul> |
| 91 | </li> |
| 92 | </ul> |
| 93 | <ul> |
| 94 | <li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul> |
| 95 | <li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li> |
| 96 | <li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">Controllers</a></li> |
| 97 | <li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">Reserved Names</a></li> |
| 98 | <li class="toctree-l2"><a class="reference internal" href="../general/views.html">Views</a></li> |
| 99 | <li class="toctree-l2"><a class="reference internal" href="../general/models.html">Models</a></li> |
| 100 | <li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helpers</a></li> |
| 101 | <li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">Using CodeIgniter Libraries</a></li> |
| 102 | <li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">Creating Libraries</a></li> |
| 103 | <li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">Using CodeIgniter Drivers</a></li> |
| 104 | <li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">Creating Drivers</a></li> |
| 105 | <li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">Creating Core System Classes</a></li> |
| 106 | <li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">Creating Ancillary Classes</a></li> |
| 107 | <li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">Hooks - Extending the Framework Core</a></li> |
| 108 | <li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">Auto-loading Resources</a></li> |
| 109 | <li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Common Functions</a></li> |
| 110 | <li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">Compatibility Functions</a></li> |
| 111 | <li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI Routing</a></li> |
| 112 | <li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li> |
| 113 | <li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Caching</a></li> |
| 114 | <li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">Profiling Your Application</a></li> |
| 115 | <li class="toctree-l2"><a class="reference internal" href="../general/cli.html">Running via the CLI</a></li> |
| 116 | <li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li> |
| 117 | <li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li> |
| 118 | <li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">Alternate PHP Syntax for View Files</a></li> |
| 119 | <li class="toctree-l2"><a class="reference internal" href="../general/security.html">Security</a></li> |
| 120 | <li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP Style Guide</a></li> |
| 121 | </ul> |
| 122 | </li> |
| 123 | </ul> |
| 124 | <ul class="current"> |
| 125 | <li class="toctree-l1 current"><a class="reference internal" href="index.html">Libraries</a><ul class="current"> |
| 126 | <li class="toctree-l2"><a class="reference internal" href="benchmark.html">Benchmarking Class</a></li> |
| 127 | <li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li> |
| 128 | <li class="toctree-l2"><a class="reference internal" href="calendar.html">Calendaring Class</a></li> |
| 129 | <li class="toctree-l2"><a class="reference internal" href="cart.html">Shopping Cart Class</a></li> |
| 130 | <li class="toctree-l2"><a class="reference internal" href="config.html">Config Class</a></li> |
| 131 | <li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li> |
| 132 | <li class="toctree-l2"><a class="reference internal" href="encrypt.html">Encrypt Class</a></li> |
| 133 | <li class="toctree-l2"><a class="reference internal" href="encryption.html">Encryption Library</a></li> |
| 134 | <li class="toctree-l2"><a class="reference internal" href="file_uploading.html">File Uploading Class</a></li> |
| 135 | <li class="toctree-l2 current"><a class="current reference internal" href="#">Form Validation</a></li> |
| 136 | <li class="toctree-l2"><a class="reference internal" href="ftp.html">FTP Class</a></li> |
| 137 | <li class="toctree-l2"><a class="reference internal" href="image_lib.html">Image Manipulation Class</a></li> |
| 138 | <li class="toctree-l2"><a class="reference internal" href="input.html">Input Class</a></li> |
| 139 | <li class="toctree-l2"><a class="reference internal" href="javascript.html">Javascript Class</a></li> |
| 140 | <li class="toctree-l2"><a class="reference internal" href="language.html">Language Class</a></li> |
| 141 | <li class="toctree-l2"><a class="reference internal" href="loader.html">Loader Class</a></li> |
| 142 | <li class="toctree-l2"><a class="reference internal" href="migration.html">Migrations Class</a></li> |
| 143 | <li class="toctree-l2"><a class="reference internal" href="output.html">Output Class</a></li> |
| 144 | <li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination Class</a></li> |
| 145 | <li class="toctree-l2"><a class="reference internal" href="parser.html">Template Parser Class</a></li> |
| 146 | <li class="toctree-l2"><a class="reference internal" href="security.html">Security Class</a></li> |
| 147 | <li class="toctree-l2"><a class="reference internal" href="sessions.html">Session Library</a></li> |
| 148 | <li class="toctree-l2"><a class="reference internal" href="table.html">HTML Table Class</a></li> |
| 149 | <li class="toctree-l2"><a class="reference internal" href="trackback.html">Trackback Class</a></li> |
| 150 | <li class="toctree-l2"><a class="reference internal" href="typography.html">Typography Class</a></li> |
| 151 | <li class="toctree-l2"><a class="reference internal" href="unit_testing.html">Unit Testing Class</a></li> |
| 152 | <li class="toctree-l2"><a class="reference internal" href="uri.html">URI Class</a></li> |
| 153 | <li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li> |
| 154 | <li class="toctree-l2"><a class="reference internal" href="xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li> |
| 155 | <li class="toctree-l2"><a class="reference internal" href="zip.html">Zip Encoding Class</a></li> |
| 156 | </ul> |
| 157 | </li> |
| 158 | </ul> |
| 159 | <ul> |
| 160 | <li class="toctree-l1"><a class="reference internal" href="../database/index.html">Database Reference</a><ul> |
| 161 | <li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li> |
| 162 | <li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li> |
| 163 | <li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li> |
| 164 | <li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li> |
| 165 | <li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li> |
| 166 | <li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li> |
| 167 | <li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li> |
| 168 | <li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li> |
| 169 | <li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li> |
| 170 | <li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li> |
| 171 | <li class="toctree-l2"><a class="reference internal" href="../database/caching.html">Query Caching</a></li> |
| 172 | <li class="toctree-l2"><a class="reference internal" href="../database/forge.html">Database Manipulation with Database Forge</a></li> |
| 173 | <li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities Class</a></li> |
| 174 | <li class="toctree-l2"><a class="reference internal" href="../database/db_driver_reference.html">Database Driver Reference</a></li> |
| 175 | </ul> |
| 176 | </li> |
| 177 | </ul> |
| 178 | <ul> |
| 179 | <li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul> |
| 180 | <li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li> |
| 181 | <li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">CAPTCHA Helper</a></li> |
| 182 | <li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li> |
| 183 | <li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li> |
| 184 | <li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">Directory Helper</a></li> |
| 185 | <li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li> |
| 186 | <li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li> |
| 187 | <li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File Helper</a></li> |
| 188 | <li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li> |
| 189 | <li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li> |
| 190 | <li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li> |
| 191 | <li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">Language Helper</a></li> |
| 192 | <li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li> |
| 193 | <li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">Path Helper</a></li> |
| 194 | <li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li> |
| 195 | <li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">Smiley Helper</a></li> |
| 196 | <li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String Helper</a></li> |
| 197 | <li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li> |
| 198 | <li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">Typography Helper</a></li> |
| 199 | <li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li> |
| 200 | <li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li> |
| 201 | </ul> |
| 202 | </li> |
| 203 | </ul> |
| 204 | |
| 205 | </div> |
| 206 | |
| 207 | |
| 208 | </div> |
| 209 | </div> |
| 210 | <div id="nav2"> |
| 211 | <a href="#" id="openToc"> |
| 212 | <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAARgAA/+4ADkFkb2JlAGTAAAAAAf/bAIQABAMDAwMDBAMDBAYEAwQGBwUEBAUHCAYGBwYGCAoICQkJCQgKCgwMDAwMCgwMDQ0MDBERERERFBQUFBQUFBQUFAEEBQUIBwgPCgoPFA4ODhQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU/8AAEQgAKwCaAwERAAIRAQMRAf/EAHsAAQAABwEBAAAAAAAAAAAAAAABAwQFBgcIAgkBAQAAAAAAAAAAAAAAAAAAAAAQAAEDAwICBwYEAgsAAAAAAAIBAwQAEQUSBiEHkROTVNQWGDFBUVIUCHEiMtOUFWGBobHRQlMkZIRVEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwDSC+ygkOOaUoKigUCgUCgUCgUCgUCgUCgUCgkuGguIP9FBMFb0Hqg7We+3jlmIqqYFf4ub+/QYlnOR/LqIBKGFUbf8qWv971BytQXXE7Y3Lnm3HsFhp2TaZJAdchRXpIgSpdEJWxJEW3xoKV7F5OMy7JkQn2o7D6w33XGjEAkoiqrJEqIiOIiKuhePCgqp22dyYyS3CyWHnQ5joG61HkRnmnTbaFSMhExRVQRRVJU9iUHjE7ez+fJ0MFipmUNhBV8YUd2SoIV9KkjQla9ltegttBdPLW4/qocL+UTfrMiHW4+P9M71shuyrqaHTcxsl7jegpsji8nh5ZwMvDfgTm0RTjSmjYdFCS6KoOIipdFunCgmNYTMv457MMY6U7iI6oMieDDhRm1VbIhuoOkbqtuK0Hpzb+eZcYZexUxt6UyUqK2cd0SdjtgrhOgijcgERUlJOCIl6CpgbP3blRI8XgMjNARAyKNDfeRBdFDBVUAXgQrqH4pxoJTu2NysY97LP4ac1io5q1InHFeGO24LnVKJuKOkSQ/yKir+rh7aCLG1dzypZQI2FnvTgccYOM3FeN0XWERXAUEFVQgQkUktdLpegm+Td3/Xli/L+S/mYNJIOF9G/wBeLKrZHFb0akG6W1WtQWSg3Dyg5e7V3fipE3O4/wCrktyzYA+ufas2LbZIlmnAT2kvuoN1wft95augilglX/tzP3qCu9O3LL/wV/i5v79BvmTADq14UGu91467Z6U9y0HzH/ncj/U/sT/CgynZG7I2NezpZGUjIycJkYkZSG+uQ81pbBNKLxJfjwoMqZ3/ALYHl35AJ7/cuwHcu5k7r1Q5pHetBjquqVVJWGxj9Zrtcl/Ggy3dHMvauR3HFZj5nHNxSyW5JISYDMoIwx8tFIGHZhPNaykGapr6rUAiicEoMG21lMRj8buPAz8xhJrr7uOeiPTCyAwXUaGR1mgozbTusOsFLEiJ7fbQa/h7gcjy2H3V6xppwDNtUSxCJIqp7valBuWVzJ22xuCROXNNZiJkMtms0DbjUkAZjzoDrTMd9dDRI44ZC2YsrYdKWP2WDT2S3N9dNdlRYrGMYc06IURXSYb0igrpWS485xVNS6nF4rwslkoMwnbpgZLB7bmt5uMweAhDEl4B5uSLzzqTnnyVpW2jaJHRMSIjdDiiotvy3DOE5rYTEbkl5yFn28k7JyG4c7AU2HtLH1uKfaiMPI40CdYbpNtmLdwTSn5rewLNld+7TLdeal4WarWBkbVKBjgdElMJJwAAY5fl4kB3b1fp4XvagsGS3FjJfLzDNtS8aeXx7LzT7TyzByQE5PccRGRC0ZRUDRV6y62vbjagzLmJzS2vuPK43JY6aP1TW6Jz+RIWyFtyC06y3EkiiinAo7YCqfq1AqqnGgsOH3lhZO8d1pmcpB8j5XIm9OYlBJSQ/FSS4427DKO0RC8AlcEMhFdViRR1WDWR5t3WXVuL1d106kG9vdeye2g60+1FDyW0shIcXVpyroXt8I8dfd+NB1vioAdWnD3UF1+gD4UFc6CEKpagxXN43rwJLUHz7yX2c8zokt9uHlsPIhA4aRnnHJTLptIS6CNsY7iASpxUUMkReGpfbQW0vtN5pitvrsN28rwtBD0nc0+/Yft5XhaB6TuaXfsP28rwtA9J3NPv2H7eV4Wgek7mn37D9vK8LQPSdzT79h+3leFoHpO5pd+w/byvC0D0nc0u/Yft5XhaB6TuaXfsP28rwtA9J3NLv2H7eV4Wgek7ml37D9vK8LQPSdzS79h+3leFoHpO5p9+w/byvC0E9r7Reazy2HIYVPxkS/CUHVn26cosxyv2g7h89LYmZSXOenvLEQ1YaQ222RATcQCP8rSGqqA8S02W2pQ6FhMoAIlqCtsnwoCpdKClejI4i3Sgtb+GBxVuNBSFt1pV/RQefLjPyUDy4z8lA8uM/JQPLjPyUDy4z8lA8uM/JQPLjPyUDy4z8lA8uM/JQPLjPyUDy4z8lA8utJ/koJ7WCbBU/LQXOPAFq1koK8B0pag90CggtBBf6qB0UDooHRQOigdFA6KB0UDooHRQOigdFA6KB0UDooI0EaBQf//Z" title="Toggle Table of Contents" alt="Toggle Table of Contents" /> |
| 213 | </a> |
| 214 | </div> |
| 215 | |
| 216 | <div class="wy-grid-for-nav"> |
| 217 | |
| 218 | |
| 219 | <nav data-toggle="wy-nav-shift" class="wy-nav-side"> |
| 220 | <div class="wy-side-nav-search"> |
| 221 | |
| 222 | <a href="../index.html" class="fa fa-home"> CodeIgniter</a> |
| 223 | |
| 224 | |
| 225 | <div role="search"> |
| 226 | <form id="rtd-search-form" class="wy-form" action="../search.html" method="get"> |
| 227 | <input type="text" name="q" placeholder="Search docs" /> |
| 228 | <input type="hidden" name="check_keywords" value="yes" /> |
| 229 | <input type="hidden" name="area" value="default" /> |
| 230 | </form> |
| 231 | </div> |
| 232 | </div> |
| 233 | |
| 234 | <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> |
| 235 | |
| 236 | |
| 237 | |
| 238 | <ul> |
| 239 | <li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">Welcome to CodeIgniter</a></li> |
| 240 | </ul> |
| 241 | <ul> |
| 242 | <li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul> |
| 243 | <li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li> |
| 244 | <li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</a></li> |
| 245 | <li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li> |
| 246 | <li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li> |
| 247 | </ul> |
| 248 | </li> |
| 249 | </ul> |
| 250 | <ul> |
| 251 | <li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul> |
| 252 | <li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li> |
| 253 | <li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li> |
| 254 | <li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li> |
| 255 | <li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li> |
| 256 | <li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li> |
| 257 | <li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li> |
| 258 | </ul> |
| 259 | </li> |
| 260 | </ul> |
| 261 | <ul> |
| 262 | <li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a><ul> |
| 263 | <li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li> |
| 264 | <li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li> |
| 265 | <li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li> |
| 266 | <li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li> |
| 267 | </ul> |
| 268 | </li> |
| 269 | </ul> |
| 270 | <ul> |
| 271 | <li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing to CodeIgniter</a><ul> |
| 272 | <li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li> |
| 273 | <li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</a></li> |
| 274 | </ul> |
| 275 | </li> |
| 276 | </ul> |
| 277 | <ul> |
| 278 | <li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul> |
| 279 | <li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li> |
| 280 | <li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">Controllers</a></li> |
| 281 | <li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">Reserved Names</a></li> |
| 282 | <li class="toctree-l2"><a class="reference internal" href="../general/views.html">Views</a></li> |
| 283 | <li class="toctree-l2"><a class="reference internal" href="../general/models.html">Models</a></li> |
| 284 | <li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helpers</a></li> |
| 285 | <li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">Using CodeIgniter Libraries</a></li> |
| 286 | <li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">Creating Libraries</a></li> |
| 287 | <li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">Using CodeIgniter Drivers</a></li> |
| 288 | <li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">Creating Drivers</a></li> |
| 289 | <li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">Creating Core System Classes</a></li> |
| 290 | <li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">Creating Ancillary Classes</a></li> |
| 291 | <li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">Hooks - Extending the Framework Core</a></li> |
| 292 | <li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">Auto-loading Resources</a></li> |
| 293 | <li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Common Functions</a></li> |
| 294 | <li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">Compatibility Functions</a></li> |
| 295 | <li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI Routing</a></li> |
| 296 | <li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li> |
| 297 | <li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Caching</a></li> |
| 298 | <li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">Profiling Your Application</a></li> |
| 299 | <li class="toctree-l2"><a class="reference internal" href="../general/cli.html">Running via the CLI</a></li> |
| 300 | <li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li> |
| 301 | <li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li> |
| 302 | <li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">Alternate PHP Syntax for View Files</a></li> |
| 303 | <li class="toctree-l2"><a class="reference internal" href="../general/security.html">Security</a></li> |
| 304 | <li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP Style Guide</a></li> |
| 305 | </ul> |
| 306 | </li> |
| 307 | </ul> |
| 308 | <ul class="current"> |
| 309 | <li class="toctree-l1 current"><a class="reference internal" href="index.html">Libraries</a><ul class="current"> |
| 310 | <li class="toctree-l2"><a class="reference internal" href="benchmark.html">Benchmarking Class</a></li> |
| 311 | <li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li> |
| 312 | <li class="toctree-l2"><a class="reference internal" href="calendar.html">Calendaring Class</a></li> |
| 313 | <li class="toctree-l2"><a class="reference internal" href="cart.html">Shopping Cart Class</a></li> |
| 314 | <li class="toctree-l2"><a class="reference internal" href="config.html">Config Class</a></li> |
| 315 | <li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li> |
| 316 | <li class="toctree-l2"><a class="reference internal" href="encrypt.html">Encrypt Class</a></li> |
| 317 | <li class="toctree-l2"><a class="reference internal" href="encryption.html">Encryption Library</a></li> |
| 318 | <li class="toctree-l2"><a class="reference internal" href="file_uploading.html">File Uploading Class</a></li> |
| 319 | <li class="toctree-l2 current"><a class="current reference internal" href="#">Form Validation</a></li> |
| 320 | <li class="toctree-l2"><a class="reference internal" href="ftp.html">FTP Class</a></li> |
| 321 | <li class="toctree-l2"><a class="reference internal" href="image_lib.html">Image Manipulation Class</a></li> |
| 322 | <li class="toctree-l2"><a class="reference internal" href="input.html">Input Class</a></li> |
| 323 | <li class="toctree-l2"><a class="reference internal" href="javascript.html">Javascript Class</a></li> |
| 324 | <li class="toctree-l2"><a class="reference internal" href="language.html">Language Class</a></li> |
| 325 | <li class="toctree-l2"><a class="reference internal" href="loader.html">Loader Class</a></li> |
| 326 | <li class="toctree-l2"><a class="reference internal" href="migration.html">Migrations Class</a></li> |
| 327 | <li class="toctree-l2"><a class="reference internal" href="output.html">Output Class</a></li> |
| 328 | <li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination Class</a></li> |
| 329 | <li class="toctree-l2"><a class="reference internal" href="parser.html">Template Parser Class</a></li> |
| 330 | <li class="toctree-l2"><a class="reference internal" href="security.html">Security Class</a></li> |
| 331 | <li class="toctree-l2"><a class="reference internal" href="sessions.html">Session Library</a></li> |
| 332 | <li class="toctree-l2"><a class="reference internal" href="table.html">HTML Table Class</a></li> |
| 333 | <li class="toctree-l2"><a class="reference internal" href="trackback.html">Trackback Class</a></li> |
| 334 | <li class="toctree-l2"><a class="reference internal" href="typography.html">Typography Class</a></li> |
| 335 | <li class="toctree-l2"><a class="reference internal" href="unit_testing.html">Unit Testing Class</a></li> |
| 336 | <li class="toctree-l2"><a class="reference internal" href="uri.html">URI Class</a></li> |
| 337 | <li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li> |
| 338 | <li class="toctree-l2"><a class="reference internal" href="xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li> |
| 339 | <li class="toctree-l2"><a class="reference internal" href="zip.html">Zip Encoding Class</a></li> |
| 340 | </ul> |
| 341 | </li> |
| 342 | </ul> |
| 343 | <ul> |
| 344 | <li class="toctree-l1"><a class="reference internal" href="../database/index.html">Database Reference</a><ul> |
| 345 | <li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li> |
| 346 | <li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li> |
| 347 | <li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li> |
| 348 | <li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li> |
| 349 | <li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li> |
| 350 | <li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li> |
| 351 | <li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li> |
| 352 | <li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li> |
| 353 | <li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li> |
| 354 | <li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li> |
| 355 | <li class="toctree-l2"><a class="reference internal" href="../database/caching.html">Query Caching</a></li> |
| 356 | <li class="toctree-l2"><a class="reference internal" href="../database/forge.html">Database Manipulation with Database Forge</a></li> |
| 357 | <li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities Class</a></li> |
| 358 | <li class="toctree-l2"><a class="reference internal" href="../database/db_driver_reference.html">Database Driver Reference</a></li> |
| 359 | </ul> |
| 360 | </li> |
| 361 | </ul> |
| 362 | <ul> |
| 363 | <li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul> |
| 364 | <li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li> |
| 365 | <li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">CAPTCHA Helper</a></li> |
| 366 | <li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li> |
| 367 | <li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li> |
| 368 | <li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">Directory Helper</a></li> |
| 369 | <li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li> |
| 370 | <li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li> |
| 371 | <li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File Helper</a></li> |
| 372 | <li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li> |
| 373 | <li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li> |
| 374 | <li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li> |
| 375 | <li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">Language Helper</a></li> |
| 376 | <li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li> |
| 377 | <li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">Path Helper</a></li> |
| 378 | <li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li> |
| 379 | <li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">Smiley Helper</a></li> |
| 380 | <li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String Helper</a></li> |
| 381 | <li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li> |
| 382 | <li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">Typography Helper</a></li> |
| 383 | <li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li> |
| 384 | <li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li> |
| 385 | </ul> |
| 386 | </li> |
| 387 | </ul> |
| 388 | |
| 389 | |
| 390 | |
| 391 | </div> |
| 392 | |
| 393 | </nav> |
| 394 | |
| 395 | <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> |
| 396 | |
| 397 | |
| 398 | <nav class="wy-nav-top" role="navigation" aria-label="top navigation"> |
| 399 | <i data-toggle="wy-nav-top" class="fa fa-bars"></i> |
| 400 | <a href="../index.html">CodeIgniter</a> |
| 401 | </nav> |
| 402 | |
| 403 | |
| 404 | |
| 405 | <div class="wy-nav-content"> |
| 406 | <div class="rst-content"> |
| 407 | <div role="navigation" aria-label="breadcrumbs navigation"> |
| 408 | <ul class="wy-breadcrumbs"> |
| 409 | <li><a href="../index.html">Docs</a> »</li> |
| 410 | |
| 411 | <li><a href="index.html">Libraries</a> »</li> |
| 412 | |
| 413 | <li>Form Validation</li> |
| 414 | <li class="wy-breadcrumbs-aside"> |
| 415 | |
| 416 | </li> |
| 417 | <div style="float:right;margin-left:5px;" id="closeMe"> |
| 418 | <img title="Classic Layout" alt="classic layout" src="data:image/gif;base64,R0lGODlhFAAUAJEAAAAAADMzM////wAAACH5BAUUAAIALAAAAAAUABQAAAImlI+py+0PU5gRBRDM3DxbWoXis42X13USOLauUIqnlsaH/eY6UwAAOw==" /> |
| 419 | </div> |
| 420 | </ul> |
| 421 | <hr/> |
| 422 | </div> |
| 423 | <div role="main" class="document"> |
| 424 | |
| 425 | <div class="section" id="form-validation"> |
| 426 | <h1><a class="toc-backref" href="#id6">Form Validation</a><a class="headerlink" href="#form-validation" title="Permalink to this headline">¶</a></h1> |
| 427 | <p>CodeIgniter provides a comprehensive form validation and data prepping |
| 428 | class that helps minimize the amount of code you’ll write.</p> |
| 429 | <div class="contents topic" id="page-contents"> |
| 430 | <p class="topic-title first">Page Contents</p> |
| 431 | <ul class="simple"> |
| 432 | <li><a class="reference internal" href="#form-validation" id="id6">Form Validation</a><ul> |
| 433 | <li><a class="reference internal" href="#overview" id="id7">Overview</a></li> |
| 434 | <li><a class="reference internal" href="#form-validation-tutorial" id="id8">Form Validation Tutorial</a><ul> |
| 435 | <li><a class="reference internal" href="#the-form" id="id9">The Form</a></li> |
| 436 | <li><a class="reference internal" href="#the-success-page" id="id10">The Success Page</a></li> |
| 437 | <li><a class="reference internal" href="#the-controller" id="id11">The Controller</a></li> |
| 438 | <li><a class="reference internal" href="#try-it" id="id12">Try it!</a></li> |
| 439 | <li><a class="reference internal" href="#explanation" id="id13">Explanation</a></li> |
| 440 | <li><a class="reference internal" href="#setting-validation-rules" id="id14">Setting Validation Rules</a></li> |
| 441 | <li><a class="reference internal" href="#setting-rules-using-an-array" id="id15">Setting Rules Using an Array</a></li> |
| 442 | <li><a class="reference internal" href="#cascading-rules" id="id16">Cascading Rules</a></li> |
| 443 | <li><a class="reference internal" href="#prepping-data" id="id17">Prepping Data</a></li> |
| 444 | <li><a class="reference internal" href="#re-populating-the-form" id="id18">Re-populating the form</a></li> |
| 445 | <li><a class="reference internal" href="#callbacks-your-own-validation-methods" id="id19">Callbacks: Your own Validation Methods</a></li> |
| 446 | <li><a class="reference internal" href="#callable-use-anything-as-a-rule" id="id20">Callable: Use anything as a rule</a></li> |
| 447 | <li><a class="reference internal" href="#setting-error-messages" id="id21">Setting Error Messages</a></li> |
| 448 | <li><a class="reference internal" href="#translating-field-names" id="id22">Translating Field Names</a></li> |
| 449 | <li><a class="reference internal" href="#changing-the-error-delimiters" id="id23">Changing the Error Delimiters</a></li> |
| 450 | <li><a class="reference internal" href="#showing-errors-individually" id="id24">Showing Errors Individually</a></li> |
| 451 | <li><a class="reference internal" href="#validating-an-array-other-than-post" id="id25">Validating an Array (other than $_POST)</a></li> |
| 452 | </ul> |
| 453 | </li> |
| 454 | <li><a class="reference internal" href="#saving-sets-of-validation-rules-to-a-config-file" id="id26">Saving Sets of Validation Rules to a Config File</a><ul> |
| 455 | <li><a class="reference internal" href="#how-to-save-your-rules" id="id27">How to save your rules</a></li> |
| 456 | <li><a class="reference internal" href="#creating-sets-of-rules" id="id28">Creating Sets of Rules</a></li> |
| 457 | <li><a class="reference internal" href="#calling-a-specific-rule-group" id="id29">Calling a Specific Rule Group</a></li> |
| 458 | <li><a class="reference internal" href="#associating-a-controller-method-with-a-rule-group" id="id30">Associating a Controller Method with a Rule Group</a></li> |
| 459 | </ul> |
| 460 | </li> |
| 461 | <li><a class="reference internal" href="#using-arrays-as-field-names" id="id31">Using Arrays as Field Names</a></li> |
| 462 | <li><a class="reference internal" href="#rule-reference" id="id32">Rule Reference</a></li> |
| 463 | <li><a class="reference internal" href="#prepping-reference" id="id33">Prepping Reference</a></li> |
| 464 | <li><a class="reference internal" href="#class-reference" id="id34">Class Reference</a></li> |
| 465 | <li><a class="reference internal" href="#helper-reference" id="id35">Helper Reference</a></li> |
| 466 | </ul> |
| 467 | </li> |
| 468 | </ul> |
| 469 | </div> |
| 470 | <div class="section" id="overview"> |
| 471 | <h2><a class="toc-backref" href="#id7">Overview</a><a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2> |
| 472 | <p>Before explaining CodeIgniter’s approach to data validation, let’s |
| 473 | describe the ideal scenario:</p> |
| 474 | <ol class="arabic simple"> |
| 475 | <li>A form is displayed.</li> |
| 476 | <li>You fill it in and submit it.</li> |
| 477 | <li>If you submitted something invalid, or perhaps missed a required |
| 478 | item, the form is redisplayed containing your data along with an |
| 479 | error message describing the problem.</li> |
| 480 | <li>This process continues until you have submitted a valid form.</li> |
| 481 | </ol> |
| 482 | <p>On the receiving end, the script must:</p> |
| 483 | <ol class="arabic simple"> |
| 484 | <li>Check for required data.</li> |
| 485 | <li>Verify that the data is of the correct type, and meets the correct |
| 486 | criteria. For example, if a username is submitted it must be |
| 487 | validated to contain only permitted characters. It must be of a |
| 488 | minimum length, and not exceed a maximum length. The username can’t |
| 489 | be someone else’s existing username, or perhaps even a reserved word. |
| 490 | Etc.</li> |
| 491 | <li>Sanitize the data for security.</li> |
| 492 | <li>Pre-format the data if needed (Does the data need to be trimmed? HTML |
| 493 | encoded? Etc.)</li> |
| 494 | <li>Prep the data for insertion in the database.</li> |
| 495 | </ol> |
| 496 | <p>Although there is nothing terribly complex about the above process, it |
| 497 | usually requires a significant amount of code, and to display error |
| 498 | messages, various control structures are usually placed within the form |
| 499 | HTML. Form validation, while simple to create, is generally very messy |
| 500 | and tedious to implement.</p> |
| 501 | </div> |
| 502 | <div class="section" id="form-validation-tutorial"> |
| 503 | <h2><a class="toc-backref" href="#id8">Form Validation Tutorial</a><a class="headerlink" href="#form-validation-tutorial" title="Permalink to this headline">¶</a></h2> |
| 504 | <p>What follows is a “hands on” tutorial for implementing CodeIgniters Form |
| 505 | Validation.</p> |
| 506 | <p>In order to implement form validation you’ll need three things:</p> |
| 507 | <ol class="arabic simple"> |
| 508 | <li>A <a class="reference internal" href="../general/views.html"><span class="doc">View</span></a> file containing a form.</li> |
| 509 | <li>A View file containing a “success” message to be displayed upon |
| 510 | successful submission.</li> |
| 511 | <li>A <a class="reference internal" href="../general/controllers.html"><span class="doc">controller</span></a> method to receive and |
| 512 | process the submitted data.</li> |
| 513 | </ol> |
| 514 | <p>Let’s create those three things, using a member sign-up form as the |
| 515 | example.</p> |
| 516 | <div class="section" id="the-form"> |
| 517 | <h3><a class="toc-backref" href="#id9">The Form</a><a class="headerlink" href="#the-form" title="Permalink to this headline">¶</a></h3> |
| 518 | <p>Using a text editor, create a form called myform.php. In it, place this |
| 519 | code and save it to your application/views/ folder:</p> |
| 520 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">html</span><span class="o">></span> |
| 521 | <span class="o"><</span><span class="nx">head</span><span class="o">></span> |
| 522 | <span class="o"><</span><span class="nx">title</span><span class="o">></span><span class="nx">My</span> <span class="nx">Form</span><span class="o"></</span><span class="nx">title</span><span class="o">></span> |
| 523 | <span class="o"></</span><span class="nx">head</span><span class="o">></span> |
| 524 | <span class="o"><</span><span class="nx">body</span><span class="o">></span> |
| 525 | |
| 526 | <span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">validation_errors</span><span class="p">();</span> <span class="cp">?></span> |
| 527 | |
| 528 | <span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">form_open</span><span class="p">(</span><span class="s1">'form'</span><span class="p">);</span> <span class="cp">?></span> |
| 529 | |
| 530 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Username<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 531 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"username"</span> <span class="na">value</span><span class="o">=</span><span class="s">""</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 532 | |
| 533 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Password<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 534 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"password"</span> <span class="na">value</span><span class="o">=</span><span class="s">""</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 535 | |
| 536 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Password Confirm<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 537 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"passconf"</span> <span class="na">value</span><span class="o">=</span><span class="s">""</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 538 | |
| 539 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Email Address<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 540 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"email"</span> <span class="na">value</span><span class="o">=</span><span class="s">""</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 541 | |
| 542 | <span class="p"><</span><span class="nt">div</span><span class="p">><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"submit"</span> <span class="na">value</span><span class="o">=</span><span class="s">"Submit"</span> <span class="p">/></</span><span class="nt">div</span><span class="p">></span> |
| 543 | |
| 544 | <span class="p"></</span><span class="nt">form</span><span class="p">></span> |
| 545 | |
| 546 | <span class="p"></</span><span class="nt">body</span><span class="p">></span> |
| 547 | <span class="p"></</span><span class="nt">html</span><span class="p">></span> |
| 548 | </pre></div> |
| 549 | </div> |
| 550 | </div> |
| 551 | <div class="section" id="the-success-page"> |
| 552 | <h3><a class="toc-backref" href="#id10">The Success Page</a><a class="headerlink" href="#the-success-page" title="Permalink to this headline">¶</a></h3> |
| 553 | <p>Using a text editor, create a form called formsuccess.php. In it, place |
| 554 | this code and save it to your application/views/ folder:</p> |
| 555 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">html</span><span class="o">></span> |
| 556 | <span class="o"><</span><span class="nx">head</span><span class="o">></span> |
| 557 | <span class="o"><</span><span class="nx">title</span><span class="o">></span><span class="nx">My</span> <span class="nx">Form</span><span class="o"></</span><span class="nx">title</span><span class="o">></span> |
| 558 | <span class="o"></</span><span class="nx">head</span><span class="o">></span> |
| 559 | <span class="o"><</span><span class="nx">body</span><span class="o">></span> |
| 560 | |
| 561 | <span class="o"><</span><span class="nx">h3</span><span class="o">></span><span class="nx">Your</span> <span class="nx">form</span> <span class="nx">was</span> <span class="nx">successfully</span> <span class="nx">submitted</span><span class="o">!</</span><span class="nx">h3</span><span class="o">></span> |
| 562 | |
| 563 | <span class="o"><</span><span class="nx">p</span><span class="o">><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">anchor</span><span class="p">(</span><span class="s1">'form'</span><span class="p">,</span> <span class="s1">'Try it again!'</span><span class="p">);</span> <span class="cp">?></span><span class="p"></</span><span class="nt">p</span><span class="p">></span> |
| 564 | |
| 565 | <span class="p"></</span><span class="nt">body</span><span class="p">></span> |
| 566 | <span class="p"></</span><span class="nt">html</span><span class="p">></span> |
| 567 | </pre></div> |
| 568 | </div> |
| 569 | </div> |
| 570 | <div class="section" id="the-controller"> |
| 571 | <h3><a class="toc-backref" href="#id11">The Controller</a><a class="headerlink" href="#the-controller" title="Permalink to this headline">¶</a></h3> |
| 572 | <p>Using a text editor, create a controller called Form.php. In it, place |
| 573 | this code and save it to your application/controllers/ folder:</p> |
| 574 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> |
| 575 | |
| 576 | <span class="k">class</span> <span class="nc">Form</span> <span class="k">extends</span> <span class="nx">CI_Controller</span> <span class="p">{</span> |
| 577 | |
| 578 | <span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span> |
| 579 | <span class="p">{</span> |
| 580 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">helper</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'form'</span><span class="p">,</span> <span class="s1">'url'</span><span class="p">));</span> |
| 581 | |
| 582 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">library</span><span class="p">(</span><span class="s1">'form_validation'</span><span class="p">);</span> |
| 583 | |
| 584 | <span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">run</span><span class="p">()</span> <span class="o">==</span> <span class="k">FALSE</span><span class="p">)</span> |
| 585 | <span class="p">{</span> |
| 586 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'myform'</span><span class="p">);</span> |
| 587 | <span class="p">}</span> |
| 588 | <span class="k">else</span> |
| 589 | <span class="p">{</span> |
| 590 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'formsuccess'</span><span class="p">);</span> |
| 591 | <span class="p">}</span> |
| 592 | <span class="p">}</span> |
| 593 | <span class="p">}</span> |
| 594 | </pre></div> |
| 595 | </div> |
| 596 | </div> |
| 597 | <div class="section" id="try-it"> |
| 598 | <h3><a class="toc-backref" href="#id12">Try it!</a><a class="headerlink" href="#try-it" title="Permalink to this headline">¶</a></h3> |
| 599 | <p>To try your form, visit your site using a URL similar to this one:</p> |
| 600 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">index</span><span class="o">.</span><span class="nx">php</span><span class="o">/</span><span class="nx">form</span><span class="o">/</span> |
| 601 | </pre></div> |
| 602 | </div> |
| 603 | <p>If you submit the form you should simply see the form reload. That’s |
| 604 | because you haven’t set up any validation rules yet.</p> |
| 605 | <p><strong>Since you haven’t told the Form Validation class to validate anything |
| 606 | yet, it returns FALSE (boolean false) by default. ``The run()`` method |
| 607 | only returns TRUE if it has successfully applied your rules without any |
| 608 | of them failing.</strong></p> |
| 609 | </div> |
| 610 | <div class="section" id="explanation"> |
| 611 | <h3><a class="toc-backref" href="#id13">Explanation</a><a class="headerlink" href="#explanation" title="Permalink to this headline">¶</a></h3> |
| 612 | <p>You’ll notice several things about the above pages:</p> |
| 613 | <p>The form (myform.php) is a standard web form with a couple exceptions:</p> |
| 614 | <ol class="arabic"> |
| 615 | <li><p class="first">It uses a form helper to create the form opening. Technically, this |
| 616 | isn’t necessary. You could create the form using standard HTML. |
| 617 | However, the benefit of using the helper is that it generates the |
| 618 | action URL for you, based on the URL in your config file. This makes |
| 619 | your application more portable in the event your URLs change.</p> |
| 620 | </li> |
| 621 | <li><p class="first">At the top of the form you’ll notice the following function call:</p> |
| 622 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">validation_errors</span><span class="p">();</span> <span class="cp">?></span> |
| 623 | </pre></div> |
| 624 | </div> |
| 625 | <p>This function will return any error messages sent back by the |
| 626 | validator. If there are no messages it returns an empty string.</p> |
| 627 | </li> |
| 628 | </ol> |
| 629 | <p>The controller (Form.php) has one method: <code class="docutils literal"><span class="pre">index()</span></code>. This method |
| 630 | initializes the validation class and loads the form helper and URL |
| 631 | helper used by your view files. It also runs the validation routine. |
| 632 | Based on whether the validation was successful it either presents the |
| 633 | form or the success page.</p> |
| 634 | </div> |
| 635 | <div class="section" id="setting-validation-rules"> |
| 636 | <span id="id1"></span><h3><a class="toc-backref" href="#id14">Setting Validation Rules</a><a class="headerlink" href="#setting-validation-rules" title="Permalink to this headline">¶</a></h3> |
| 637 | <p>CodeIgniter lets you set as many validation rules as you need for a |
| 638 | given field, cascading them in order, and it even lets you prep and |
| 639 | pre-process the field data at the same time. To set validation rules you |
| 640 | will use the <code class="docutils literal"><span class="pre">set_rules()</span></code> method:</p> |
| 641 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">();</span> |
| 642 | </pre></div> |
| 643 | </div> |
| 644 | <p>The above method takes <strong>three</strong> parameters as input:</p> |
| 645 | <ol class="arabic simple"> |
| 646 | <li>The field name - the exact name you’ve given the form field.</li> |
| 647 | <li>A “human” name for this field, which will be inserted into the error |
| 648 | message. For example, if your field is named “user” you might give it |
| 649 | a human name of “Username”.</li> |
| 650 | <li>The validation rules for this form field.</li> |
| 651 | <li>(optional) Set custom error messages on any rules given for current field. If not provided will use the default one.</li> |
| 652 | </ol> |
| 653 | <div class="admonition note"> |
| 654 | <p class="first admonition-title">Note</p> |
| 655 | <p class="last">If you would like the field name to be stored in a language |
| 656 | file, please see <a class="reference internal" href="#translating-field-names"><span class="std std-ref">Translating Field Names</span></a>.</p> |
| 657 | </div> |
| 658 | <p>Here is an example. In your controller (Form.php), add this code just |
| 659 | below the validation initialization method:</p> |
| 660 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 661 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'password'</span><span class="p">,</span> <span class="s1">'Password'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 662 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'passconf'</span><span class="p">,</span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 663 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'email'</span><span class="p">,</span> <span class="s1">'Email'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 664 | </pre></div> |
| 665 | </div> |
| 666 | <p>Your controller should now look like this:</p> |
| 667 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> |
| 668 | |
| 669 | <span class="k">class</span> <span class="nc">Form</span> <span class="k">extends</span> <span class="nx">CI_Controller</span> <span class="p">{</span> |
| 670 | |
| 671 | <span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span> |
| 672 | <span class="p">{</span> |
| 673 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">helper</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'form'</span><span class="p">,</span> <span class="s1">'url'</span><span class="p">));</span> |
| 674 | |
| 675 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">library</span><span class="p">(</span><span class="s1">'form_validation'</span><span class="p">);</span> |
| 676 | |
| 677 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 678 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'password'</span><span class="p">,</span> <span class="s1">'Password'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">,</span> |
| 679 | <span class="k">array</span><span class="p">(</span><span class="s1">'required'</span> <span class="o">=></span> <span class="s1">'You must provide a %s.'</span><span class="p">)</span> |
| 680 | <span class="p">);</span> |
| 681 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'passconf'</span><span class="p">,</span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 682 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'email'</span><span class="p">,</span> <span class="s1">'Email'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 683 | |
| 684 | <span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">run</span><span class="p">()</span> <span class="o">==</span> <span class="k">FALSE</span><span class="p">)</span> |
| 685 | <span class="p">{</span> |
| 686 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'myform'</span><span class="p">);</span> |
| 687 | <span class="p">}</span> |
| 688 | <span class="k">else</span> |
| 689 | <span class="p">{</span> |
| 690 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'formsuccess'</span><span class="p">);</span> |
| 691 | <span class="p">}</span> |
| 692 | <span class="p">}</span> |
| 693 | <span class="p">}</span> |
| 694 | </pre></div> |
| 695 | </div> |
| 696 | <p>Now submit the form with the fields blank and you should see the error |
| 697 | messages. If you submit the form with all the fields populated you’ll |
| 698 | see your success page.</p> |
| 699 | <div class="admonition note"> |
| 700 | <p class="first admonition-title">Note</p> |
| 701 | <p class="last">The form fields are not yet being re-populated with the data |
| 702 | when there is an error. We’ll get to that shortly.</p> |
| 703 | </div> |
| 704 | </div> |
| 705 | <div class="section" id="setting-rules-using-an-array"> |
| 706 | <h3><a class="toc-backref" href="#id15">Setting Rules Using an Array</a><a class="headerlink" href="#setting-rules-using-an-array" title="Permalink to this headline">¶</a></h3> |
| 707 | <p>Before moving on it should be noted that the rule setting method can |
| 708 | be passed an array if you prefer to set all your rules in one action. If |
| 709 | you use this approach, you must name your array keys as indicated:</p> |
| 710 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$config</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span> |
| 711 | <span class="k">array</span><span class="p">(</span> |
| 712 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'username'</span><span class="p">,</span> |
| 713 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Username'</span><span class="p">,</span> |
| 714 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 715 | <span class="p">),</span> |
| 716 | <span class="k">array</span><span class="p">(</span> |
| 717 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'password'</span><span class="p">,</span> |
| 718 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password'</span><span class="p">,</span> |
| 719 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span><span class="p">,</span> |
| 720 | <span class="s1">'errors'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span> |
| 721 | <span class="s1">'required'</span> <span class="o">=></span> <span class="s1">'You must provide a %s.'</span><span class="p">,</span> |
| 722 | <span class="p">),</span> |
| 723 | <span class="p">),</span> |
| 724 | <span class="k">array</span><span class="p">(</span> |
| 725 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'passconf'</span><span class="p">,</span> |
| 726 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> |
| 727 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 728 | <span class="p">),</span> |
| 729 | <span class="k">array</span><span class="p">(</span> |
| 730 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'email'</span><span class="p">,</span> |
| 731 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Email'</span><span class="p">,</span> |
| 732 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 733 | <span class="p">)</span> |
| 734 | <span class="p">);</span> |
| 735 | |
| 736 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="nv">$config</span><span class="p">);</span> |
| 737 | </pre></div> |
| 738 | </div> |
| 739 | </div> |
| 740 | <div class="section" id="cascading-rules"> |
| 741 | <h3><a class="toc-backref" href="#id16">Cascading Rules</a><a class="headerlink" href="#cascading-rules" title="Permalink to this headline">¶</a></h3> |
| 742 | <p>CodeIgniter lets you pipe multiple rules together. Let’s try it. Change |
| 743 | your rules in the third parameter of rule setting method, like this:</p> |
| 744 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span> |
| 745 | <span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> |
| 746 | <span class="s1">'required|min_length[5]|max_length[12]|is_unique[users.username]'</span><span class="p">,</span> |
| 747 | <span class="k">array</span><span class="p">(</span> |
| 748 | <span class="s1">'required'</span> <span class="o">=></span> <span class="s1">'You have not provided %s.'</span><span class="p">,</span> |
| 749 | <span class="s1">'is_unique'</span> <span class="o">=></span> <span class="s1">'This %s already exists.'</span> |
| 750 | <span class="p">)</span> |
| 751 | <span class="p">);</span> |
| 752 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'password'</span><span class="p">,</span> <span class="s1">'Password'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 753 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'passconf'</span><span class="p">,</span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> <span class="s1">'required|matches[password]'</span><span class="p">);</span> |
| 754 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'email'</span><span class="p">,</span> <span class="s1">'Email'</span><span class="p">,</span> <span class="s1">'required|valid_email|is_unique[users.email]'</span><span class="p">);</span> |
| 755 | </pre></div> |
| 756 | </div> |
| 757 | <p>The above code sets the following rules:</p> |
| 758 | <ol class="arabic simple"> |
| 759 | <li>The username field be no shorter than 5 characters and no longer than |
| 760 | 12.</li> |
| 761 | <li>The password field must match the password confirmation field.</li> |
| 762 | <li>The email field must contain a valid email address.</li> |
| 763 | </ol> |
| 764 | <p>Give it a try! Submit your form without the proper data and you’ll see |
| 765 | new error messages that correspond to your new rules. There are numerous |
| 766 | rules available which you can read about in the validation reference.</p> |
| 767 | <div class="admonition note"> |
| 768 | <p class="first admonition-title">Note</p> |
| 769 | <p>You can also pass an array of rules to <code class="docutils literal"><span class="pre">set_rules()</span></code>, |
| 770 | instead of a string. Example:</p> |
| 771 | <div class="last highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="s1">'required'</span><span class="p">,</span> <span class="s1">'min_length[5]'</span><span class="p">));</span> |
| 772 | </pre></div> |
| 773 | </div> |
| 774 | </div> |
| 775 | </div> |
| 776 | <div class="section" id="prepping-data"> |
| 777 | <h3><a class="toc-backref" href="#id17">Prepping Data</a><a class="headerlink" href="#prepping-data" title="Permalink to this headline">¶</a></h3> |
| 778 | <p>In addition to the validation method like the ones we used above, you |
| 779 | can also prep your data in various ways. For example, you can set up |
| 780 | rules like this:</p> |
| 781 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> <span class="s1">'trim|required|min_length[5]|max_length[12]'</span><span class="p">);</span> |
| 782 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'password'</span><span class="p">,</span> <span class="s1">'Password'</span><span class="p">,</span> <span class="s1">'trim|required|min_length[8]'</span><span class="p">);</span> |
| 783 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'passconf'</span><span class="p">,</span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> <span class="s1">'trim|required|matches[password]'</span><span class="p">);</span> |
| 784 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'email'</span><span class="p">,</span> <span class="s1">'Email'</span><span class="p">,</span> <span class="s1">'trim|required|valid_email'</span><span class="p">);</span> |
| 785 | </pre></div> |
| 786 | </div> |
| 787 | <p>In the above example, we are “trimming” the fields, checking for length |
| 788 | where necessary and making sure that both password fields match.</p> |
| 789 | <p><strong>Any native PHP function that accepts one parameter can be used as a |
| 790 | rule, like ``htmlspecialchars()``, ``trim()``, etc.</strong></p> |
| 791 | <div class="admonition note"> |
| 792 | <p class="first admonition-title">Note</p> |
| 793 | <p class="last">You will generally want to use the prepping functions |
| 794 | <strong>after</strong> the validation rules so if there is an error, the |
| 795 | original data will be shown in the form.</p> |
| 796 | </div> |
| 797 | </div> |
| 798 | <div class="section" id="re-populating-the-form"> |
| 799 | <h3><a class="toc-backref" href="#id18">Re-populating the form</a><a class="headerlink" href="#re-populating-the-form" title="Permalink to this headline">¶</a></h3> |
| 800 | <p>Thus far we have only been dealing with errors. It’s time to repopulate |
| 801 | the form field with the submitted data. CodeIgniter offers several |
| 802 | helper functions that permit you to do this. The one you will use most |
| 803 | commonly is:</p> |
| 804 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nx">set_value</span><span class="p">(</span><span class="s1">'field name'</span><span class="p">)</span> |
| 805 | </pre></div> |
| 806 | </div> |
| 807 | <p>Open your myform.php view file and update the <strong>value</strong> in each field |
| 808 | using the <a class="reference internal" href="../helpers/form_helper.html#set_value" title="set_value"><code class="xref php php-func docutils literal"><span class="pre">set_value()</span></code></a> function:</p> |
| 809 | <p><strong>Don’t forget to include each field name in the :php:func:`set_value()` |
| 810 | function calls!</strong></p> |
| 811 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">html</span><span class="o">></span> |
| 812 | <span class="o"><</span><span class="nx">head</span><span class="o">></span> |
| 813 | <span class="o"><</span><span class="nx">title</span><span class="o">></span><span class="nx">My</span> <span class="nx">Form</span><span class="o"></</span><span class="nx">title</span><span class="o">></span> |
| 814 | <span class="o"></</span><span class="nx">head</span><span class="o">></span> |
| 815 | <span class="o"><</span><span class="nx">body</span><span class="o">></span> |
| 816 | |
| 817 | <span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">validation_errors</span><span class="p">();</span> <span class="cp">?></span> |
| 818 | |
| 819 | <span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">form_open</span><span class="p">(</span><span class="s1">'form'</span><span class="p">);</span> <span class="cp">?></span> |
| 820 | |
| 821 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Username<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 822 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"username"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">set_value</span><span class="p">(</span><span class="s1">'username'</span><span class="p">);</span> <span class="cp">?></span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 823 | |
| 824 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Password<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 825 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"password"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">set_value</span><span class="p">(</span><span class="s1">'password'</span><span class="p">);</span> <span class="cp">?></span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 826 | |
| 827 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Password Confirm<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 828 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"passconf"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">set_value</span><span class="p">(</span><span class="s1">'passconf'</span><span class="p">);</span> <span class="cp">?></span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 829 | |
| 830 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Email Address<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 831 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"email"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">set_value</span><span class="p">(</span><span class="s1">'email'</span><span class="p">);</span> <span class="cp">?></span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 832 | |
| 833 | <span class="p"><</span><span class="nt">div</span><span class="p">><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"submit"</span> <span class="na">value</span><span class="o">=</span><span class="s">"Submit"</span> <span class="p">/></</span><span class="nt">div</span><span class="p">></span> |
| 834 | |
| 835 | <span class="p"></</span><span class="nt">form</span><span class="p">></span> |
| 836 | |
| 837 | <span class="p"></</span><span class="nt">body</span><span class="p">></span> |
| 838 | <span class="p"></</span><span class="nt">html</span><span class="p">></span> |
| 839 | </pre></div> |
| 840 | </div> |
| 841 | <p>Now reload your page and submit the form so that it triggers an error. |
| 842 | Your form fields should now be re-populated</p> |
| 843 | <div class="admonition note"> |
| 844 | <p class="first admonition-title">Note</p> |
| 845 | <p class="last">The <a class="reference internal" href="#class-reference"><span class="std std-ref">Class Reference</span></a> section below |
| 846 | contains methods that permit you to re-populate <select> menus, |
| 847 | radio buttons, and checkboxes.</p> |
| 848 | </div> |
| 849 | <div class="admonition important"> |
| 850 | <p class="first admonition-title">Important</p> |
| 851 | <p>If you use an array as the name of a form field, you |
| 852 | must supply it as an array to the function. Example:</p> |
| 853 | <div class="last highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"text"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"colors[]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">"<?php echo set_value('colors[]'); ?>"</span> <span class="nx">size</span><span class="o">=</span><span class="s2">"50"</span> <span class="o">/></span> |
| 854 | </pre></div> |
| 855 | </div> |
| 856 | </div> |
| 857 | <p>For more info please see the <a class="reference internal" href="#using-arrays-as-field-names"><span class="std std-ref">Using Arrays as Field Names</span></a> section below.</p> |
| 858 | </div> |
| 859 | <div class="section" id="callbacks-your-own-validation-methods"> |
| 860 | <h3><a class="toc-backref" href="#id19">Callbacks: Your own Validation Methods</a><a class="headerlink" href="#callbacks-your-own-validation-methods" title="Permalink to this headline">¶</a></h3> |
| 861 | <p>The validation system supports callbacks to your own validation |
| 862 | methods. This permits you to extend the validation class to meet your |
| 863 | needs. For example, if you need to run a database query to see if the |
| 864 | user is choosing a unique username, you can create a callback method |
| 865 | that does that. Let’s create an example of this.</p> |
| 866 | <p>In your controller, change the “username” rule to this:</p> |
| 867 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> <span class="s1">'callback_username_check'</span><span class="p">);</span> |
| 868 | </pre></div> |
| 869 | </div> |
| 870 | <p>Then add a new method called <code class="docutils literal"><span class="pre">username_check()</span></code> to your controller. |
| 871 | Here’s how your controller should now look:</p> |
| 872 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> |
| 873 | |
| 874 | <span class="k">class</span> <span class="nc">Form</span> <span class="k">extends</span> <span class="nx">CI_Controller</span> <span class="p">{</span> |
| 875 | |
| 876 | <span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span> |
| 877 | <span class="p">{</span> |
| 878 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">helper</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="s1">'form'</span><span class="p">,</span> <span class="s1">'url'</span><span class="p">));</span> |
| 879 | |
| 880 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">library</span><span class="p">(</span><span class="s1">'form_validation'</span><span class="p">);</span> |
| 881 | |
| 882 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> <span class="s1">'callback_username_check'</span><span class="p">);</span> |
| 883 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'password'</span><span class="p">,</span> <span class="s1">'Password'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 884 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'passconf'</span><span class="p">,</span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 885 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'email'</span><span class="p">,</span> <span class="s1">'Email'</span><span class="p">,</span> <span class="s1">'required|is_unique[users.email]'</span><span class="p">);</span> |
| 886 | |
| 887 | <span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">run</span><span class="p">()</span> <span class="o">==</span> <span class="k">FALSE</span><span class="p">)</span> |
| 888 | <span class="p">{</span> |
| 889 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'myform'</span><span class="p">);</span> |
| 890 | <span class="p">}</span> |
| 891 | <span class="k">else</span> |
| 892 | <span class="p">{</span> |
| 893 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'formsuccess'</span><span class="p">);</span> |
| 894 | <span class="p">}</span> |
| 895 | <span class="p">}</span> |
| 896 | |
| 897 | <span class="k">public</span> <span class="k">function</span> <span class="nf">username_check</span><span class="p">(</span><span class="nv">$str</span><span class="p">)</span> |
| 898 | <span class="p">{</span> |
| 899 | <span class="k">if</span> <span class="p">(</span><span class="nv">$str</span> <span class="o">==</span> <span class="s1">'test'</span><span class="p">)</span> |
| 900 | <span class="p">{</span> |
| 901 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_message</span><span class="p">(</span><span class="s1">'username_check'</span><span class="p">,</span> <span class="s1">'The {field} field can not be the word "test"'</span><span class="p">);</span> |
| 902 | <span class="k">return</span> <span class="k">FALSE</span><span class="p">;</span> |
| 903 | <span class="p">}</span> |
| 904 | <span class="k">else</span> |
| 905 | <span class="p">{</span> |
| 906 | <span class="k">return</span> <span class="k">TRUE</span><span class="p">;</span> |
| 907 | <span class="p">}</span> |
| 908 | <span class="p">}</span> |
| 909 | |
| 910 | <span class="p">}</span> |
| 911 | </pre></div> |
| 912 | </div> |
| 913 | <p>Reload your form and submit it with the word “test” as the username. You |
| 914 | can see that the form field data was passed to your callback method |
| 915 | for you to process.</p> |
| 916 | <p>To invoke a callback just put the method name in a rule, with |
| 917 | “callback_” as the rule <strong>prefix</strong>. If you need to receive an extra |
| 918 | parameter in your callback method, just add it normally after the |
| 919 | method name between square brackets, as in: <code class="docutils literal"><span class="pre">callback_foo[bar]</span></code>, |
| 920 | then it will be passed as the second argument of your callback method.</p> |
| 921 | <div class="admonition note"> |
| 922 | <p class="first admonition-title">Note</p> |
| 923 | <p class="last">You can also process the form data that is passed to your |
| 924 | callback and return it. If your callback returns anything other than a |
| 925 | boolean TRUE/FALSE it is assumed that the data is your newly processed |
| 926 | form data.</p> |
| 927 | </div> |
| 928 | </div> |
| 929 | <div class="section" id="callable-use-anything-as-a-rule"> |
| 930 | <h3><a class="toc-backref" href="#id20">Callable: Use anything as a rule</a><a class="headerlink" href="#callable-use-anything-as-a-rule" title="Permalink to this headline">¶</a></h3> |
| 931 | <p>If callback rules aren’t good enough for you (for example, because they are |
| 932 | limited to your controller), don’t get disappointed, there’s one more way |
| 933 | to create custom rules: anything that <code class="docutils literal"><span class="pre">is_callable()</span></code> would return TRUE for.</p> |
| 934 | <p>Consider the following example:</p> |
| 935 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span> |
| 936 | <span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> |
| 937 | <span class="k">array</span><span class="p">(</span> |
| 938 | <span class="s1">'required'</span><span class="p">,</span> |
| 939 | <span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">users_model</span><span class="p">,</span> <span class="s1">'valid_username'</span><span class="p">)</span> |
| 940 | <span class="p">)</span> |
| 941 | <span class="p">);</span> |
| 942 | </pre></div> |
| 943 | </div> |
| 944 | <p>The above code would use the <code class="docutils literal"><span class="pre">valid_username()</span></code> method from your |
| 945 | <code class="docutils literal"><span class="pre">Users_model</span></code> object.</p> |
| 946 | <p>This is just an example of course, and callbacks aren’t limited to models. |
| 947 | You can use any object/method that accepts the field value as its’ first |
| 948 | parameter. You can also use an anonymous function:</p> |
| 949 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span> |
| 950 | <span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> |
| 951 | <span class="k">array</span><span class="p">(</span> |
| 952 | <span class="s1">'required'</span><span class="p">,</span> |
| 953 | <span class="k">function</span><span class="p">(</span><span class="nv">$value</span><span class="p">)</span> |
| 954 | <span class="p">{</span> |
| 955 | <span class="c1">// Check $value</span> |
| 956 | <span class="p">}</span> |
| 957 | <span class="p">)</span> |
| 958 | <span class="p">);</span> |
| 959 | </pre></div> |
| 960 | </div> |
| 961 | <p>Of course, since a Callable rule by itself is not a string, it isn’t |
| 962 | a rule name either. That is a problem when you want to set error messages |
| 963 | for them. In order to get around that problem, you can put such rules as |
| 964 | the second element of an array, with the first one being the rule name:</p> |
| 965 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span> |
| 966 | <span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> |
| 967 | <span class="k">array</span><span class="p">(</span> |
| 968 | <span class="s1">'required'</span><span class="p">,</span> |
| 969 | <span class="k">array</span><span class="p">(</span><span class="s1">'username_callable'</span><span class="p">,</span> <span class="k">array</span><span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">users_model</span><span class="p">,</span> <span class="s1">'valid_username'</span><span class="p">))</span> |
| 970 | <span class="p">)</span> |
| 971 | <span class="p">);</span> |
| 972 | </pre></div> |
| 973 | </div> |
| 974 | <p>Anonymous function version:</p> |
| 975 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span> |
| 976 | <span class="s1">'username'</span><span class="p">,</span> <span class="s1">'Username'</span><span class="p">,</span> |
| 977 | <span class="k">array</span><span class="p">(</span> |
| 978 | <span class="s1">'required'</span><span class="p">,</span> |
| 979 | <span class="k">array</span><span class="p">(</span> |
| 980 | <span class="s1">'username_callable'</span><span class="p">,</span> |
| 981 | <span class="k">function</span><span class="p">(</span><span class="nv">$str</span><span class="p">)</span> |
| 982 | <span class="p">{</span> |
| 983 | <span class="c1">// Check validity of $str and return TRUE or FALSE</span> |
| 984 | <span class="p">}</span> |
| 985 | <span class="p">)</span> |
| 986 | <span class="p">)</span> |
| 987 | <span class="p">);</span> |
| 988 | </pre></div> |
| 989 | </div> |
| 990 | </div> |
| 991 | <div class="section" id="setting-error-messages"> |
| 992 | <span id="id2"></span><h3><a class="toc-backref" href="#id21">Setting Error Messages</a><a class="headerlink" href="#setting-error-messages" title="Permalink to this headline">¶</a></h3> |
| 993 | <p>All of the native error messages are located in the following language |
| 994 | file: <strong>system/language/english/form_validation_lang.php</strong></p> |
| 995 | <p>To set your own global custom message for a rule, you can either |
| 996 | extend/override the language file by creating your own in |
| 997 | <strong>application/language/english/form_validation_lang.php</strong> (read more |
| 998 | about this in the <a class="reference internal" href="language.html"><span class="doc">Language Class</span></a> documentation), |
| 999 | or use the following method:</p> |
| 1000 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_message</span><span class="p">(</span><span class="s1">'rule'</span><span class="p">,</span> <span class="s1">'Error Message'</span><span class="p">);</span> |
| 1001 | </pre></div> |
| 1002 | </div> |
| 1003 | <p>If you need to set a custom error message for a particular field on |
| 1004 | some particular rule, use the set_rules() method:</p> |
| 1005 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'field_name'</span><span class="p">,</span> <span class="s1">'Field Label'</span><span class="p">,</span> <span class="s1">'rule1|rule2|rule3'</span><span class="p">,</span> |
| 1006 | <span class="k">array</span><span class="p">(</span><span class="s1">'rule2'</span> <span class="o">=></span> <span class="s1">'Error Message on rule2 for this field_name'</span><span class="p">)</span> |
| 1007 | <span class="p">);</span> |
| 1008 | </pre></div> |
| 1009 | </div> |
| 1010 | <p>Where rule corresponds to the name of a particular rule, and Error |
| 1011 | Message is the text you would like displayed.</p> |
| 1012 | <p>If you’d like to include a field’s “human” name, or the optional |
| 1013 | parameter some rules allow for (such as max_length), you can add the |
| 1014 | <strong>{field}</strong> and <strong>{param}</strong> tags to your message, respectively:</p> |
| 1015 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_message</span><span class="p">(</span><span class="s1">'min_length'</span><span class="p">,</span> <span class="s1">'{field} must have at least {param} characters.'</span><span class="p">);</span> |
| 1016 | </pre></div> |
| 1017 | </div> |
| 1018 | <p>On a field with the human name Username and a rule of min_length[5], an |
| 1019 | error would display: “Username must have at least 5 characters.”</p> |
| 1020 | <div class="admonition note"> |
| 1021 | <p class="first admonition-title">Note</p> |
| 1022 | <p class="last">The old <cite>sprintf()</cite> method of using <strong>%s</strong> in your error messages |
| 1023 | will still work, however it will override the tags above. You should |
| 1024 | use one or the other.</p> |
| 1025 | </div> |
| 1026 | <p>In the callback rule example above, the error message was set by passing |
| 1027 | the name of the method (without the “callback_” prefix):</p> |
| 1028 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_message</span><span class="p">(</span><span class="s1">'username_check'</span><span class="p">)</span> |
| 1029 | </pre></div> |
| 1030 | </div> |
| 1031 | </div> |
| 1032 | <div class="section" id="translating-field-names"> |
| 1033 | <span id="id3"></span><h3><a class="toc-backref" href="#id22">Translating Field Names</a><a class="headerlink" href="#translating-field-names" title="Permalink to this headline">¶</a></h3> |
| 1034 | <p>If you would like to store the “human” name you passed to the |
| 1035 | <code class="docutils literal"><span class="pre">set_rules()</span></code> method in a language file, and therefore make the name |
| 1036 | able to be translated, here’s how:</p> |
| 1037 | <p>First, prefix your “human” name with <strong>lang:</strong>, as in this example:</p> |
| 1038 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'first_name'</span><span class="p">,</span> <span class="s1">'lang:first_name'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 1039 | </pre></div> |
| 1040 | </div> |
| 1041 | <p>Then, store the name in one of your language file arrays (without the |
| 1042 | prefix):</p> |
| 1043 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$lang</span><span class="p">[</span><span class="s1">'first_name'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'First Name'</span><span class="p">;</span> |
| 1044 | </pre></div> |
| 1045 | </div> |
| 1046 | <div class="admonition note"> |
| 1047 | <p class="first admonition-title">Note</p> |
| 1048 | <p>If you store your array item in a language file that is not |
| 1049 | loaded automatically by CI, you’ll need to remember to load it in your |
| 1050 | controller using:</p> |
| 1051 | <div class="last highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">lang</span><span class="o">-></span><span class="na">load</span><span class="p">(</span><span class="s1">'file_name'</span><span class="p">);</span> |
| 1052 | </pre></div> |
| 1053 | </div> |
| 1054 | </div> |
| 1055 | <p>See the <a class="reference internal" href="language.html"><span class="doc">Language Class</span></a> page for more info regarding |
| 1056 | language files.</p> |
| 1057 | </div> |
| 1058 | <div class="section" id="changing-the-error-delimiters"> |
| 1059 | <span id="changing-delimiters"></span><h3><a class="toc-backref" href="#id23">Changing the Error Delimiters</a><a class="headerlink" href="#changing-the-error-delimiters" title="Permalink to this headline">¶</a></h3> |
| 1060 | <p>By default, the Form Validation class adds a paragraph tag (<p>) around |
| 1061 | each error message shown. You can either change these delimiters |
| 1062 | globally, individually, or change the defaults in a config file.</p> |
| 1063 | <ol class="arabic"> |
| 1064 | <li><p class="first"><strong>Changing delimiters Globally</strong> |
| 1065 | To globally change the error delimiters, in your controller method, |
| 1066 | just after loading the Form Validation class, add this:</p> |
| 1067 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_error_delimiters</span><span class="p">(</span><span class="s1">'<div class="error">'</span><span class="p">,</span> <span class="s1">'</div>'</span><span class="p">);</span> |
| 1068 | </pre></div> |
| 1069 | </div> |
| 1070 | <p>In this example, we’ve switched to using div tags.</p> |
| 1071 | </li> |
| 1072 | <li><p class="first"><strong>Changing delimiters Individually</strong> |
| 1073 | Each of the two error generating functions shown in this tutorial can |
| 1074 | be supplied their own delimiters as follows:</p> |
| 1075 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">form_error</span><span class="p">(</span><span class="s1">'field name'</span><span class="p">,</span> <span class="s1">'<div class="error">'</span><span class="p">,</span> <span class="s1">'</div>'</span><span class="p">);</span> <span class="cp">?></span> |
| 1076 | </pre></div> |
| 1077 | </div> |
| 1078 | <p>Or:</p> |
| 1079 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">validation_errors</span><span class="p">(</span><span class="s1">'<div class="error">'</span><span class="p">,</span> <span class="s1">'</div>'</span><span class="p">);</span> <span class="cp">?></span> |
| 1080 | </pre></div> |
| 1081 | </div> |
| 1082 | </li> |
| 1083 | <li><p class="first"><strong>Set delimiters in a config file</strong> |
| 1084 | You can add your error delimiters in application/config/form_validation.php as follows:</p> |
| 1085 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$config</span><span class="p">[</span><span class="s1">'error_prefix'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'<div class="error_prefix">'</span><span class="p">;</span> |
| 1086 | <span class="nv">$config</span><span class="p">[</span><span class="s1">'error_suffix'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'</div>'</span><span class="p">;</span> |
| 1087 | </pre></div> |
| 1088 | </div> |
| 1089 | </li> |
| 1090 | </ol> |
| 1091 | </div> |
| 1092 | <div class="section" id="showing-errors-individually"> |
| 1093 | <h3><a class="toc-backref" href="#id24">Showing Errors Individually</a><a class="headerlink" href="#showing-errors-individually" title="Permalink to this headline">¶</a></h3> |
| 1094 | <p>If you prefer to show an error message next to each form field, rather |
| 1095 | than as a list, you can use the <a class="reference internal" href="../helpers/form_helper.html#form_error" title="form_error"><code class="xref php php-func docutils literal"><span class="pre">form_error()</span></code></a> function.</p> |
| 1096 | <p>Try it! Change your form so that it looks like this:</p> |
| 1097 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">h5</span><span class="o">></span><span class="nx">Username</span><span class="o"></</span><span class="nx">h5</span><span class="o">></span> |
| 1098 | <span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">form_error</span><span class="p">(</span><span class="s1">'username'</span><span class="p">);</span> <span class="cp">?></span> |
| 1099 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"username"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">set_value</span><span class="p">(</span><span class="s1">'username'</span><span class="p">);</span> <span class="cp">?></span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 1100 | |
| 1101 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Password<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 1102 | <span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">form_error</span><span class="p">(</span><span class="s1">'password'</span><span class="p">);</span> <span class="cp">?></span> |
| 1103 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"password"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">set_value</span><span class="p">(</span><span class="s1">'password'</span><span class="p">);</span> <span class="cp">?></span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 1104 | |
| 1105 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Password Confirm<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 1106 | <span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">form_error</span><span class="p">(</span><span class="s1">'passconf'</span><span class="p">);</span> <span class="cp">?></span> |
| 1107 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"passconf"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">set_value</span><span class="p">(</span><span class="s1">'passconf'</span><span class="p">);</span> <span class="cp">?></span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 1108 | |
| 1109 | <span class="p"><</span><span class="nt">h5</span><span class="p">></span>Email Address<span class="p"></</span><span class="nt">h5</span><span class="p">></span> |
| 1110 | <span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">form_error</span><span class="p">(</span><span class="s1">'email'</span><span class="p">);</span> <span class="cp">?></span> |
| 1111 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"email"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">set_value</span><span class="p">(</span><span class="s1">'email'</span><span class="p">);</span> <span class="cp">?></span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 1112 | </pre></div> |
| 1113 | </div> |
| 1114 | <p>If there are no errors, nothing will be shown. If there is an error, the |
| 1115 | message will appear.</p> |
| 1116 | <div class="admonition important"> |
| 1117 | <p class="first admonition-title">Important</p> |
| 1118 | <p>If you use an array as the name of a form field, you |
| 1119 | must supply it as an array to the function. Example:</p> |
| 1120 | <div class="last highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">form_error</span><span class="p">(</span><span class="s1">'options[size]'</span><span class="p">);</span> <span class="cp">?></span> |
| 1121 | <span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"text"</span> <span class="na">name</span><span class="o">=</span><span class="s">"options[size]"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp"><?php</span> <span class="k">echo</span> <span class="nx">set_value</span><span class="p">(</span><span class="s2">"options[size]"</span><span class="p">);</span> <span class="cp">?></span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"50"</span> <span class="p">/></span> |
| 1122 | </pre></div> |
| 1123 | </div> |
| 1124 | </div> |
| 1125 | <p>For more info please see the <a class="reference internal" href="#using-arrays-as-field-names"><span class="std std-ref">Using Arrays as Field Names</span></a> section below.</p> |
| 1126 | </div> |
| 1127 | <div class="section" id="validating-an-array-other-than-post"> |
| 1128 | <h3><a class="toc-backref" href="#id25">Validating an Array (other than $_POST)</a><a class="headerlink" href="#validating-an-array-other-than-post" title="Permalink to this headline">¶</a></h3> |
| 1129 | <p>Sometimes you may want to validate an array that does not originate from <code class="docutils literal"><span class="pre">$_POST</span></code> data.</p> |
| 1130 | <p>In this case, you can specify the array to be validated:</p> |
| 1131 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$data</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span> |
| 1132 | <span class="s1">'username'</span> <span class="o">=></span> <span class="s1">'johndoe'</span><span class="p">,</span> |
| 1133 | <span class="s1">'password'</span> <span class="o">=></span> <span class="s1">'mypassword'</span><span class="p">,</span> |
| 1134 | <span class="s1">'passconf'</span> <span class="o">=></span> <span class="s1">'mypassword'</span> |
| 1135 | <span class="p">);</span> |
| 1136 | |
| 1137 | <span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_data</span><span class="p">(</span><span class="nv">$data</span><span class="p">);</span> |
| 1138 | </pre></div> |
| 1139 | </div> |
| 1140 | <p>Creating validation rules, running the validation, and retrieving error |
| 1141 | messages works the same whether you are validating <code class="docutils literal"><span class="pre">$_POST</span></code> data or |
| 1142 | another array of your choice.</p> |
| 1143 | <div class="admonition important"> |
| 1144 | <p class="first admonition-title">Important</p> |
| 1145 | <p class="last">You have to call the <code class="docutils literal"><span class="pre">set_data()</span></code> method <em>before</em> defining |
| 1146 | any validation rules.</p> |
| 1147 | </div> |
| 1148 | <div class="admonition important"> |
| 1149 | <p class="first admonition-title">Important</p> |
| 1150 | <p class="last">If you want to validate more than one array during a single |
| 1151 | execution, then you should call the <code class="docutils literal"><span class="pre">reset_validation()</span></code> method |
| 1152 | before setting up rules and validating the new array.</p> |
| 1153 | </div> |
| 1154 | <p>For more info please see the <a class="reference internal" href="#class-reference"><span class="std std-ref">Class Reference</span></a> section below.</p> |
| 1155 | </div> |
| 1156 | </div> |
| 1157 | <div class="section" id="saving-sets-of-validation-rules-to-a-config-file"> |
| 1158 | <span id="saving-groups"></span><h2><a class="toc-backref" href="#id26">Saving Sets of Validation Rules to a Config File</a><a class="headerlink" href="#saving-sets-of-validation-rules-to-a-config-file" title="Permalink to this headline">¶</a></h2> |
| 1159 | <p>A nice feature of the Form Validation class is that it permits you to |
| 1160 | store all your validation rules for your entire application in a config |
| 1161 | file. You can organize these rules into “groups”. These groups can |
| 1162 | either be loaded automatically when a matching controller/method is |
| 1163 | called, or you can manually call each set as needed.</p> |
| 1164 | <div class="section" id="how-to-save-your-rules"> |
| 1165 | <h3><a class="toc-backref" href="#id27">How to save your rules</a><a class="headerlink" href="#how-to-save-your-rules" title="Permalink to this headline">¶</a></h3> |
| 1166 | <p>To store your validation rules, simply create a file named |
| 1167 | form_validation.php in your application/config/ folder. In that file |
| 1168 | you will place an array named $config with your rules. As shown earlier, |
| 1169 | the validation array will have this prototype:</p> |
| 1170 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$config</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span> |
| 1171 | <span class="k">array</span><span class="p">(</span> |
| 1172 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'username'</span><span class="p">,</span> |
| 1173 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Username'</span><span class="p">,</span> |
| 1174 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1175 | <span class="p">),</span> |
| 1176 | <span class="k">array</span><span class="p">(</span> |
| 1177 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'password'</span><span class="p">,</span> |
| 1178 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password'</span><span class="p">,</span> |
| 1179 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1180 | <span class="p">),</span> |
| 1181 | <span class="k">array</span><span class="p">(</span> |
| 1182 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'passconf'</span><span class="p">,</span> |
| 1183 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> |
| 1184 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1185 | <span class="p">),</span> |
| 1186 | <span class="k">array</span><span class="p">(</span> |
| 1187 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'email'</span><span class="p">,</span> |
| 1188 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Email'</span><span class="p">,</span> |
| 1189 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1190 | <span class="p">)</span> |
| 1191 | <span class="p">);</span> |
| 1192 | </pre></div> |
| 1193 | </div> |
| 1194 | <p>Your validation rule file will be loaded automatically and used when you |
| 1195 | call the <code class="docutils literal"><span class="pre">run()</span></code> method.</p> |
| 1196 | <p>Please note that you MUST name your <code class="docutils literal"><span class="pre">$config</span></code> array.</p> |
| 1197 | </div> |
| 1198 | <div class="section" id="creating-sets-of-rules"> |
| 1199 | <h3><a class="toc-backref" href="#id28">Creating Sets of Rules</a><a class="headerlink" href="#creating-sets-of-rules" title="Permalink to this headline">¶</a></h3> |
| 1200 | <p>In order to organize your rules into “sets” requires that you place them |
| 1201 | into “sub arrays”. Consider the following example, showing two sets of |
| 1202 | rules. We’ve arbitrarily called these two rules “signup” and “email”. |
| 1203 | You can name your rules anything you want:</p> |
| 1204 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$config</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span> |
| 1205 | <span class="s1">'signup'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span> |
| 1206 | <span class="k">array</span><span class="p">(</span> |
| 1207 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'username'</span><span class="p">,</span> |
| 1208 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Username'</span><span class="p">,</span> |
| 1209 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1210 | <span class="p">),</span> |
| 1211 | <span class="k">array</span><span class="p">(</span> |
| 1212 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'password'</span><span class="p">,</span> |
| 1213 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password'</span><span class="p">,</span> |
| 1214 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1215 | <span class="p">),</span> |
| 1216 | <span class="k">array</span><span class="p">(</span> |
| 1217 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'passconf'</span><span class="p">,</span> |
| 1218 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> |
| 1219 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1220 | <span class="p">),</span> |
| 1221 | <span class="k">array</span><span class="p">(</span> |
| 1222 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'email'</span><span class="p">,</span> |
| 1223 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Email'</span><span class="p">,</span> |
| 1224 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1225 | <span class="p">)</span> |
| 1226 | <span class="p">),</span> |
| 1227 | <span class="s1">'email'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span> |
| 1228 | <span class="k">array</span><span class="p">(</span> |
| 1229 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'emailaddress'</span><span class="p">,</span> |
| 1230 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'EmailAddress'</span><span class="p">,</span> |
| 1231 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required|valid_email'</span> |
| 1232 | <span class="p">),</span> |
| 1233 | <span class="k">array</span><span class="p">(</span> |
| 1234 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'name'</span><span class="p">,</span> |
| 1235 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Name'</span><span class="p">,</span> |
| 1236 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required|alpha'</span> |
| 1237 | <span class="p">),</span> |
| 1238 | <span class="k">array</span><span class="p">(</span> |
| 1239 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'title'</span><span class="p">,</span> |
| 1240 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Title'</span><span class="p">,</span> |
| 1241 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1242 | <span class="p">),</span> |
| 1243 | <span class="k">array</span><span class="p">(</span> |
| 1244 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'message'</span><span class="p">,</span> |
| 1245 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'MessageBody'</span><span class="p">,</span> |
| 1246 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1247 | <span class="p">)</span> |
| 1248 | <span class="p">)</span> |
| 1249 | <span class="p">);</span> |
| 1250 | </pre></div> |
| 1251 | </div> |
| 1252 | </div> |
| 1253 | <div class="section" id="calling-a-specific-rule-group"> |
| 1254 | <h3><a class="toc-backref" href="#id29">Calling a Specific Rule Group</a><a class="headerlink" href="#calling-a-specific-rule-group" title="Permalink to this headline">¶</a></h3> |
| 1255 | <p>In order to call a specific group, you will pass its name to the <code class="docutils literal"><span class="pre">run()</span></code> |
| 1256 | method. For example, to call the signup rule you will do this:</p> |
| 1257 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">run</span><span class="p">(</span><span class="s1">'signup'</span><span class="p">)</span> <span class="o">==</span> <span class="k">FALSE</span><span class="p">)</span> |
| 1258 | <span class="p">{</span> |
| 1259 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'myform'</span><span class="p">);</span> |
| 1260 | <span class="p">}</span> |
| 1261 | <span class="k">else</span> |
| 1262 | <span class="p">{</span> |
| 1263 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'formsuccess'</span><span class="p">);</span> |
| 1264 | <span class="p">}</span> |
| 1265 | </pre></div> |
| 1266 | </div> |
| 1267 | </div> |
| 1268 | <div class="section" id="associating-a-controller-method-with-a-rule-group"> |
| 1269 | <h3><a class="toc-backref" href="#id30">Associating a Controller Method with a Rule Group</a><a class="headerlink" href="#associating-a-controller-method-with-a-rule-group" title="Permalink to this headline">¶</a></h3> |
| 1270 | <p>An alternate (and more automatic) method of calling a rule group is to |
| 1271 | name it according to the controller class/method you intend to use it |
| 1272 | with. For example, let’s say you have a controller named Member and a |
| 1273 | method named signup. Here’s what your class might look like:</p> |
| 1274 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> |
| 1275 | |
| 1276 | <span class="k">class</span> <span class="nc">Member</span> <span class="k">extends</span> <span class="nx">CI_Controller</span> <span class="p">{</span> |
| 1277 | |
| 1278 | <span class="k">public</span> <span class="k">function</span> <span class="nf">signup</span><span class="p">()</span> |
| 1279 | <span class="p">{</span> |
| 1280 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">library</span><span class="p">(</span><span class="s1">'form_validation'</span><span class="p">);</span> |
| 1281 | |
| 1282 | <span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">run</span><span class="p">()</span> <span class="o">==</span> <span class="k">FALSE</span><span class="p">)</span> |
| 1283 | <span class="p">{</span> |
| 1284 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'myform'</span><span class="p">);</span> |
| 1285 | <span class="p">}</span> |
| 1286 | <span class="k">else</span> |
| 1287 | <span class="p">{</span> |
| 1288 | <span class="nv">$this</span><span class="o">-></span><span class="na">load</span><span class="o">-></span><span class="na">view</span><span class="p">(</span><span class="s1">'formsuccess'</span><span class="p">);</span> |
| 1289 | <span class="p">}</span> |
| 1290 | <span class="p">}</span> |
| 1291 | <span class="p">}</span> |
| 1292 | </pre></div> |
| 1293 | </div> |
| 1294 | <p>In your validation config file, you will name your rule group |
| 1295 | member/signup:</p> |
| 1296 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$config</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span> |
| 1297 | <span class="s1">'member/signup'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span> |
| 1298 | <span class="k">array</span><span class="p">(</span> |
| 1299 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'username'</span><span class="p">,</span> |
| 1300 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Username'</span><span class="p">,</span> |
| 1301 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1302 | <span class="p">),</span> |
| 1303 | <span class="k">array</span><span class="p">(</span> |
| 1304 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'password'</span><span class="p">,</span> |
| 1305 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password'</span><span class="p">,</span> |
| 1306 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1307 | <span class="p">),</span> |
| 1308 | <span class="k">array</span><span class="p">(</span> |
| 1309 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'passconf'</span><span class="p">,</span> |
| 1310 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'PasswordConfirmation'</span><span class="p">,</span> |
| 1311 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1312 | <span class="p">),</span> |
| 1313 | <span class="k">array</span><span class="p">(</span> |
| 1314 | <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'email'</span><span class="p">,</span> |
| 1315 | <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Email'</span><span class="p">,</span> |
| 1316 | <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> |
| 1317 | <span class="p">)</span> |
| 1318 | <span class="p">)</span> |
| 1319 | <span class="p">);</span> |
| 1320 | </pre></div> |
| 1321 | </div> |
| 1322 | <p>When a rule group is named identically to a controller class/method it |
| 1323 | will be used automatically when the <code class="docutils literal"><span class="pre">run()</span></code> method is invoked from that |
| 1324 | class/method.</p> |
| 1325 | </div> |
| 1326 | </div> |
| 1327 | <div class="section" id="using-arrays-as-field-names"> |
| 1328 | <span id="id4"></span><h2><a class="toc-backref" href="#id31">Using Arrays as Field Names</a><a class="headerlink" href="#using-arrays-as-field-names" title="Permalink to this headline">¶</a></h2> |
| 1329 | <p>The Form Validation class supports the use of arrays as field names. |
| 1330 | Consider this example:</p> |
| 1331 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"text"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"options[]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">""</span> <span class="nx">size</span><span class="o">=</span><span class="s2">"50"</span> <span class="o">/></span> |
| 1332 | </pre></div> |
| 1333 | </div> |
| 1334 | <p>If you do use an array as a field name, you must use the EXACT array |
| 1335 | name in the <a class="reference internal" href="#helper-functions"><span class="std std-ref">Helper Functions</span></a> that require the |
| 1336 | field name, and as your Validation Rule field name.</p> |
| 1337 | <p>For example, to set a rule for the above field you would use:</p> |
| 1338 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">set_rules</span><span class="p">(</span><span class="s1">'options[]'</span><span class="p">,</span> <span class="s1">'Options'</span><span class="p">,</span> <span class="s1">'required'</span><span class="p">);</span> |
| 1339 | </pre></div> |
| 1340 | </div> |
| 1341 | <p>Or, to show an error for the above field you would use:</p> |
| 1342 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">form_error</span><span class="p">(</span><span class="s1">'options[]'</span><span class="p">);</span> <span class="cp">?></span> |
| 1343 | </pre></div> |
| 1344 | </div> |
| 1345 | <p>Or to re-populate the field you would use:</p> |
| 1346 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"text"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"options[]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">"<?php echo set_value('options[]'); ?>"</span> <span class="nx">size</span><span class="o">=</span><span class="s2">"50"</span> <span class="o">/></span> |
| 1347 | </pre></div> |
| 1348 | </div> |
| 1349 | <p>You can use multidimensional arrays as field names as well. For example:</p> |
| 1350 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"text"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"options[size]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">""</span> <span class="nx">size</span><span class="o">=</span><span class="s2">"50"</span> <span class="o">/></span> |
| 1351 | </pre></div> |
| 1352 | </div> |
| 1353 | <p>Or even:</p> |
| 1354 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"text"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"sports[nba][basketball]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">""</span> <span class="nx">size</span><span class="o">=</span><span class="s2">"50"</span> <span class="o">/></span> |
| 1355 | </pre></div> |
| 1356 | </div> |
| 1357 | <p>As with our first example, you must use the exact array name in the |
| 1358 | helper functions:</p> |
| 1359 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">form_error</span><span class="p">(</span><span class="s1">'sports[nba][basketball]'</span><span class="p">);</span> <span class="cp">?></span> |
| 1360 | </pre></div> |
| 1361 | </div> |
| 1362 | <p>If you are using checkboxes (or other fields) that have multiple |
| 1363 | options, don’t forget to leave an empty bracket after each option, so |
| 1364 | that all selections will be added to the POST array:</p> |
| 1365 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"checkbox"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"options[]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">"red"</span> <span class="o">/></span> |
| 1366 | <span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"checkbox"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"options[]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">"blue"</span> <span class="o">/></span> |
| 1367 | <span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"checkbox"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"options[]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">"green"</span> <span class="o">/></span> |
| 1368 | </pre></div> |
| 1369 | </div> |
| 1370 | <p>Or if you use a multidimensional array:</p> |
| 1371 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"checkbox"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"options[color][]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">"red"</span> <span class="o">/></span> |
| 1372 | <span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"checkbox"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"options[color][]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">"blue"</span> <span class="o">/></span> |
| 1373 | <span class="o"><</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">"checkbox"</span> <span class="nx">name</span><span class="o">=</span><span class="s2">"options[color][]"</span> <span class="nx">value</span><span class="o">=</span><span class="s2">"green"</span> <span class="o">/></span> |
| 1374 | </pre></div> |
| 1375 | </div> |
| 1376 | <p>When you use a helper function you’ll include the bracket as well:</p> |
| 1377 | <div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> <span class="k">echo</span> <span class="nx">form_error</span><span class="p">(</span><span class="s1">'options[color][]'</span><span class="p">);</span> <span class="cp">?></span> |
| 1378 | </pre></div> |
| 1379 | </div> |
| 1380 | </div> |
| 1381 | <div class="section" id="rule-reference"> |
| 1382 | <h2><a class="toc-backref" href="#id32">Rule Reference</a><a class="headerlink" href="#rule-reference" title="Permalink to this headline">¶</a></h2> |
| 1383 | <p>The following is a list of all the native rules that are available to |
| 1384 | use:</p> |
| 1385 | <table border="1" class="docutils"> |
| 1386 | <colgroup> |
| 1387 | <col width="16%" /> |
| 1388 | <col width="7%" /> |
| 1389 | <col width="61%" /> |
| 1390 | <col width="16%" /> |
| 1391 | </colgroup> |
| 1392 | <thead valign="bottom"> |
| 1393 | <tr class="row-odd"><th class="head">Rule</th> |
| 1394 | <th class="head">Parameter</th> |
| 1395 | <th class="head">Description</th> |
| 1396 | <th class="head">Example</th> |
| 1397 | </tr> |
| 1398 | </thead> |
| 1399 | <tbody valign="top"> |
| 1400 | <tr class="row-even"><td><strong>required</strong></td> |
| 1401 | <td>No</td> |
| 1402 | <td>Returns FALSE if the form element is empty.</td> |
| 1403 | <td> </td> |
| 1404 | </tr> |
| 1405 | <tr class="row-odd"><td><strong>matches</strong></td> |
| 1406 | <td>Yes</td> |
| 1407 | <td>Returns FALSE if the form element does not match the one in the parameter.</td> |
| 1408 | <td>matches[form_item]</td> |
| 1409 | </tr> |
| 1410 | <tr class="row-even"><td><strong>regex_match</strong></td> |
| 1411 | <td>Yes</td> |
| 1412 | <td>Returns FALSE if the form element does not match the regular expression.</td> |
| 1413 | <td>regex_match[/regex/]</td> |
| 1414 | </tr> |
| 1415 | <tr class="row-odd"><td><strong>differs</strong></td> |
| 1416 | <td>Yes</td> |
| 1417 | <td>Returns FALSE if the form element does not differ from the one in the parameter.</td> |
| 1418 | <td>differs[form_item]</td> |
| 1419 | </tr> |
| 1420 | <tr class="row-even"><td><strong>is_unique</strong></td> |
| 1421 | <td>Yes</td> |
| 1422 | <td>Returns FALSE if the form element is not unique to the table and field name in the |
| 1423 | parameter. Note: This rule requires <a class="reference internal" href="../database/query_builder.html"><span class="doc">Query Builder</span></a> to be |
| 1424 | enabled in order to work.</td> |
| 1425 | <td>is_unique[table.field]</td> |
| 1426 | </tr> |
| 1427 | <tr class="row-odd"><td><strong>min_length</strong></td> |
| 1428 | <td>Yes</td> |
| 1429 | <td>Returns FALSE if the form element is shorter than the parameter value.</td> |
| 1430 | <td>min_length[3]</td> |
| 1431 | </tr> |
| 1432 | <tr class="row-even"><td><strong>max_length</strong></td> |
| 1433 | <td>Yes</td> |
| 1434 | <td>Returns FALSE if the form element is longer than the parameter value.</td> |
| 1435 | <td>max_length[12]</td> |
| 1436 | </tr> |
| 1437 | <tr class="row-odd"><td><strong>exact_length</strong></td> |
| 1438 | <td>Yes</td> |
| 1439 | <td>Returns FALSE if the form element is not exactly the parameter value.</td> |
| 1440 | <td>exact_length[8]</td> |
| 1441 | </tr> |
| 1442 | <tr class="row-even"><td><strong>greater_than</strong></td> |
| 1443 | <td>Yes</td> |
| 1444 | <td>Returns FALSE if the form element is less than or equal to the parameter value or not |
| 1445 | numeric.</td> |
| 1446 | <td>greater_than[8]</td> |
| 1447 | </tr> |
| 1448 | <tr class="row-odd"><td><strong>greater_than_equal_to</strong></td> |
| 1449 | <td>Yes</td> |
| 1450 | <td>Returns FALSE if the form element is less than the parameter value, |
| 1451 | or not numeric.</td> |
| 1452 | <td>greater_than_equal_to[8]</td> |
| 1453 | </tr> |
| 1454 | <tr class="row-even"><td><strong>less_than</strong></td> |
| 1455 | <td>Yes</td> |
| 1456 | <td>Returns FALSE if the form element is greater than or equal to the parameter value or |
| 1457 | not numeric.</td> |
| 1458 | <td>less_than[8]</td> |
| 1459 | </tr> |
| 1460 | <tr class="row-odd"><td><strong>less_than_equal_to</strong></td> |
| 1461 | <td>Yes</td> |
| 1462 | <td>Returns FALSE if the form element is greater than the parameter value, |
| 1463 | or not numeric.</td> |
| 1464 | <td>less_than_equal_to[8]</td> |
| 1465 | </tr> |
| 1466 | <tr class="row-even"><td><strong>in_list</strong></td> |
| 1467 | <td>Yes</td> |
| 1468 | <td>Returns FALSE if the form element is not within a predetermined list.</td> |
| 1469 | <td>in_list[red,blue,green]</td> |
| 1470 | </tr> |
| 1471 | <tr class="row-odd"><td><strong>alpha</strong></td> |
| 1472 | <td>No</td> |
| 1473 | <td>Returns FALSE if the form element contains anything other than alphabetical characters.</td> |
| 1474 | <td> </td> |
| 1475 | </tr> |
| 1476 | <tr class="row-even"><td><strong>alpha_numeric</strong></td> |
| 1477 | <td>No</td> |
| 1478 | <td>Returns FALSE if the form element contains anything other than alpha-numeric characters.</td> |
| 1479 | <td> </td> |
| 1480 | </tr> |
| 1481 | <tr class="row-odd"><td><strong>alpha_numeric_spaces</strong></td> |
| 1482 | <td>No</td> |
| 1483 | <td>Returns FALSE if the form element contains anything other than alpha-numeric characters |
| 1484 | or spaces. Should be used after trim to avoid spaces at the beginning or end.</td> |
| 1485 | <td> </td> |
| 1486 | </tr> |
| 1487 | <tr class="row-even"><td><strong>alpha_dash</strong></td> |
| 1488 | <td>No</td> |
| 1489 | <td>Returns FALSE if the form element contains anything other than alpha-numeric characters, |
| 1490 | underscores or dashes.</td> |
| 1491 | <td> </td> |
| 1492 | </tr> |
| 1493 | <tr class="row-odd"><td><strong>numeric</strong></td> |
| 1494 | <td>No</td> |
| 1495 | <td>Returns FALSE if the form element contains anything other than numeric characters.</td> |
| 1496 | <td> </td> |
| 1497 | </tr> |
| 1498 | <tr class="row-even"><td><strong>integer</strong></td> |
| 1499 | <td>No</td> |
| 1500 | <td>Returns FALSE if the form element contains anything other than an integer.</td> |
| 1501 | <td> </td> |
| 1502 | </tr> |
| 1503 | <tr class="row-odd"><td><strong>decimal</strong></td> |
| 1504 | <td>No</td> |
| 1505 | <td>Returns FALSE if the form element contains anything other than a decimal number.</td> |
| 1506 | <td> </td> |
| 1507 | </tr> |
| 1508 | <tr class="row-even"><td><strong>is_natural</strong></td> |
| 1509 | <td>No</td> |
| 1510 | <td>Returns FALSE if the form element contains anything other than a natural number: |
| 1511 | 0, 1, 2, 3, etc.</td> |
| 1512 | <td> </td> |
| 1513 | </tr> |
| 1514 | <tr class="row-odd"><td><strong>is_natural_no_zero</strong></td> |
| 1515 | <td>No</td> |
| 1516 | <td>Returns FALSE if the form element contains anything other than a natural |
| 1517 | number, but not zero: 1, 2, 3, etc.</td> |
| 1518 | <td> </td> |
| 1519 | </tr> |
| 1520 | <tr class="row-even"><td><strong>valid_url</strong></td> |
| 1521 | <td>No</td> |
| 1522 | <td>Returns FALSE if the form element does not contain a valid URL.</td> |
| 1523 | <td> </td> |
| 1524 | </tr> |
| 1525 | <tr class="row-odd"><td><strong>valid_email</strong></td> |
| 1526 | <td>No</td> |
| 1527 | <td>Returns FALSE if the form element does not contain a valid email address.</td> |
| 1528 | <td> </td> |
| 1529 | </tr> |
| 1530 | <tr class="row-even"><td><strong>valid_emails</strong></td> |
| 1531 | <td>No</td> |
| 1532 | <td>Returns FALSE if any value provided in a comma separated list is not a valid email.</td> |
| 1533 | <td> </td> |
| 1534 | </tr> |
| 1535 | <tr class="row-odd"><td><strong>valid_ip</strong></td> |
| 1536 | <td>Yes</td> |
| 1537 | <td>Returns FALSE if the supplied IP address is not valid. |
| 1538 | Accepts an optional parameter of ‘ipv4’ or ‘ipv6’ to specify an IP format.</td> |
| 1539 | <td> </td> |
| 1540 | </tr> |
| 1541 | <tr class="row-even"><td><strong>valid_base64</strong></td> |
| 1542 | <td>No</td> |
| 1543 | <td>Returns FALSE if the supplied string contains anything other than valid Base64 characters.</td> |
| 1544 | <td> </td> |
| 1545 | </tr> |
| 1546 | </tbody> |
| 1547 | </table> |
| 1548 | <div class="admonition note"> |
| 1549 | <p class="first admonition-title">Note</p> |
| 1550 | <p>These rules can also be called as discrete methods. For |
| 1551 | example:</p> |
| 1552 | <div class="last highlight-ci"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-></span><span class="na">form_validation</span><span class="o">-></span><span class="na">required</span><span class="p">(</span><span class="nv">$string</span><span class="p">);</span> |
| 1553 | </pre></div> |
| 1554 | </div> |
| 1555 | </div> |
| 1556 | <div class="admonition note"> |
| 1557 | <p class="first admonition-title">Note</p> |
| 1558 | <p class="last">You can also use any native PHP functions that permit up |
| 1559 | to two parameters, where at least one is required (to pass |
| 1560 | the field data).</p> |
| 1561 | </div> |
| 1562 | </div> |
| 1563 | <div class="section" id="prepping-reference"> |
| 1564 | <h2><a class="toc-backref" href="#id33">Prepping Reference</a><a class="headerlink" href="#prepping-reference" title="Permalink to this headline">¶</a></h2> |
| 1565 | <p>The following is a list of all the prepping methods that are available |
| 1566 | to use:</p> |
| 1567 | <table border="1" class="docutils"> |
| 1568 | <colgroup> |
| 1569 | <col width="14%" /> |
| 1570 | <col width="6%" /> |
| 1571 | <col width="79%" /> |
| 1572 | </colgroup> |
| 1573 | <thead valign="bottom"> |
| 1574 | <tr class="row-odd"><th class="head">Name</th> |
| 1575 | <th class="head">Parameter</th> |
| 1576 | <th class="head">Description</th> |
| 1577 | </tr> |
| 1578 | </thead> |
| 1579 | <tbody valign="top"> |
| 1580 | <tr class="row-even"><td><strong>prep_for_form</strong></td> |
| 1581 | <td>No</td> |
| 1582 | <td>DEPRECATED: Converts special characters so that HTML data can be shown in a form field without breaking it.</td> |
| 1583 | </tr> |
| 1584 | <tr class="row-odd"><td><strong>prep_url</strong></td> |
| 1585 | <td>No</td> |
| 1586 | <td>Adds “http://” to URLs if missing.</td> |
| 1587 | </tr> |
| 1588 | <tr class="row-even"><td><strong>strip_image_tags</strong></td> |
| 1589 | <td>No</td> |
| 1590 | <td>Strips the HTML from image tags leaving the raw URL.</td> |
| 1591 | </tr> |
| 1592 | <tr class="row-odd"><td><strong>encode_php_tags</strong></td> |
| 1593 | <td>No</td> |
| 1594 | <td>Converts PHP tags to entities.</td> |
| 1595 | </tr> |
| 1596 | </tbody> |
| 1597 | </table> |
| 1598 | <div class="admonition note"> |
| 1599 | <p class="first admonition-title">Note</p> |
| 1600 | <p class="last">You can also use any native PHP functions that permits one |
| 1601 | parameter, like <code class="docutils literal"><span class="pre">trim()</span></code>, <code class="docutils literal"><span class="pre">htmlspecialchars()</span></code>, <code class="docutils literal"><span class="pre">urldecode()</span></code>, |
| 1602 | etc.</p> |
| 1603 | </div> |
| 1604 | </div> |
| 1605 | <div class="section" id="class-reference"> |
| 1606 | <span id="id5"></span><h2><a class="toc-backref" href="#id34">Class Reference</a><a class="headerlink" href="#class-reference" title="Permalink to this headline">¶</a></h2> |
| 1607 | <dl class="class"> |
| 1608 | <dt id="CI_Form_validation"> |
| 1609 | <em class="property">class </em><code class="descname">CI_Form_validation</code><a class="headerlink" href="#CI_Form_validation" title="Permalink to this definition">¶</a></dt> |
| 1610 | <dd><dl class="method"> |
| 1611 | <dt id="CI_Form_validation::set_rules"> |
| 1612 | <code class="descname">set_rules</code><span class="sig-paren">(</span><em>$field</em><span class="optional">[</span>, <em>$label = ''</em><span class="optional">[</span>, <em>$rules = ''</em><span class="optional">[</span>, <em>$errors = array()</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::set_rules" title="Permalink to this definition">¶</a></dt> |
| 1613 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1614 | <col class="field-name" /> |
| 1615 | <col class="field-body" /> |
| 1616 | <tbody valign="top"> |
| 1617 | <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> |
| 1618 | <li><strong>$field</strong> (<em>string</em>) – Field name</li> |
| 1619 | <li><strong>$label</strong> (<em>string</em>) – Field label</li> |
| 1620 | <li><strong>$rules</strong> (<em>mixed</em>) – Validation rules, as a string list separated by a pipe “|”, or as an array or rules</li> |
| 1621 | <li><strong>$errors</strong> (<em>array</em>) – A list of custom error messages</li> |
| 1622 | </ul> |
| 1623 | </td> |
| 1624 | </tr> |
| 1625 | <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">CI_Form_validation instance (method chaining)</p> |
| 1626 | </td> |
| 1627 | </tr> |
| 1628 | <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CI_Form_validation</p> |
| 1629 | </td> |
| 1630 | </tr> |
| 1631 | </tbody> |
| 1632 | </table> |
| 1633 | <p>Permits you to set validation rules, as described in the tutorial |
| 1634 | sections above:</p> |
| 1635 | <ul class="simple"> |
| 1636 | <li><a class="reference internal" href="#setting-validation-rules"><span class="std std-ref">Setting Validation Rules</span></a></li> |
| 1637 | <li><a class="reference internal" href="#saving-groups"><span class="std std-ref">Saving Sets of Validation Rules to a Config File</span></a></li> |
| 1638 | </ul> |
| 1639 | </dd></dl> |
| 1640 | |
| 1641 | <dl class="method"> |
| 1642 | <dt id="CI_Form_validation::run"> |
| 1643 | <code class="descname">run</code><span class="sig-paren">(</span><span class="optional">[</span><em>$group = ''</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::run" title="Permalink to this definition">¶</a></dt> |
| 1644 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1645 | <col class="field-name" /> |
| 1646 | <col class="field-body" /> |
| 1647 | <tbody valign="top"> |
| 1648 | <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> |
| 1649 | <li><strong>$group</strong> (<em>string</em>) – The name of the validation group to run</li> |
| 1650 | </ul> |
| 1651 | </td> |
| 1652 | </tr> |
| 1653 | <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">TRUE on success, FALSE if validation failed</p> |
| 1654 | </td> |
| 1655 | </tr> |
| 1656 | <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">bool</p> |
| 1657 | </td> |
| 1658 | </tr> |
| 1659 | </tbody> |
| 1660 | </table> |
| 1661 | <p>Runs the validation routines. Returns boolean TRUE on success and FALSE |
| 1662 | on failure. You can optionally pass the name of the validation group via |
| 1663 | the method, as described in: <a class="reference internal" href="#saving-groups"><span class="std std-ref">Saving Sets of Validation Rules to a Config File</span></a></p> |
| 1664 | </dd></dl> |
| 1665 | |
| 1666 | <dl class="method"> |
| 1667 | <dt id="CI_Form_validation::set_message"> |
| 1668 | <code class="descname">set_message</code><span class="sig-paren">(</span><em>$lang</em><span class="optional">[</span>, <em>$val = ''</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::set_message" title="Permalink to this definition">¶</a></dt> |
| 1669 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1670 | <col class="field-name" /> |
| 1671 | <col class="field-body" /> |
| 1672 | <tbody valign="top"> |
| 1673 | <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> |
| 1674 | <li><strong>$lang</strong> (<em>string</em>) – The rule the message is for</li> |
| 1675 | <li><strong>$val</strong> (<em>string</em>) – The message</li> |
| 1676 | </ul> |
| 1677 | </td> |
| 1678 | </tr> |
| 1679 | <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">CI_Form_validation instance (method chaining)</p> |
| 1680 | </td> |
| 1681 | </tr> |
| 1682 | <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CI_Form_validation</p> |
| 1683 | </td> |
| 1684 | </tr> |
| 1685 | </tbody> |
| 1686 | </table> |
| 1687 | <p>Permits you to set custom error messages. See <a class="reference internal" href="#setting-error-messages"><span class="std std-ref">Setting Error Messages</span></a></p> |
| 1688 | </dd></dl> |
| 1689 | |
| 1690 | <dl class="method"> |
| 1691 | <dt id="CI_Form_validation::set_error_delimiters"> |
| 1692 | <code class="descname">set_error_delimiters</code><span class="sig-paren">(</span><span class="optional">[</span><em>$prefix = '<p>'</em><span class="optional">[</span>, <em>$suffix = '</p>'</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::set_error_delimiters" title="Permalink to this definition">¶</a></dt> |
| 1693 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1694 | <col class="field-name" /> |
| 1695 | <col class="field-body" /> |
| 1696 | <tbody valign="top"> |
| 1697 | <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> |
| 1698 | <li><strong>$prefix</strong> (<em>string</em>) – Error message prefix</li> |
| 1699 | <li><strong>$suffix</strong> (<em>string</em>) – Error message suffix</li> |
| 1700 | </ul> |
| 1701 | </td> |
| 1702 | </tr> |
| 1703 | <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">CI_Form_validation instance (method chaining)</p> |
| 1704 | </td> |
| 1705 | </tr> |
| 1706 | <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CI_Form_validation</p> |
| 1707 | </td> |
| 1708 | </tr> |
| 1709 | </tbody> |
| 1710 | </table> |
| 1711 | <p>Sets the default prefix and suffix for error messages.</p> |
| 1712 | </dd></dl> |
| 1713 | |
| 1714 | <dl class="method"> |
| 1715 | <dt id="CI_Form_validation::set_data"> |
| 1716 | <code class="descname">set_data</code><span class="sig-paren">(</span><em>$data</em><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::set_data" title="Permalink to this definition">¶</a></dt> |
| 1717 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1718 | <col class="field-name" /> |
| 1719 | <col class="field-body" /> |
| 1720 | <tbody valign="top"> |
| 1721 | <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> |
| 1722 | <li><strong>$data</strong> (<em>array</em>) – Array of data validate</li> |
| 1723 | </ul> |
| 1724 | </td> |
| 1725 | </tr> |
| 1726 | <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">CI_Form_validation instance (method chaining)</p> |
| 1727 | </td> |
| 1728 | </tr> |
| 1729 | <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CI_Form_validation</p> |
| 1730 | </td> |
| 1731 | </tr> |
| 1732 | </tbody> |
| 1733 | </table> |
| 1734 | <p>Permits you to set an array for validation, instead of using the default |
| 1735 | <code class="docutils literal"><span class="pre">$_POST</span></code> array.</p> |
| 1736 | </dd></dl> |
| 1737 | |
| 1738 | <dl class="method"> |
| 1739 | <dt id="CI_Form_validation::reset_validation"> |
| 1740 | <code class="descname">reset_validation</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::reset_validation" title="Permalink to this definition">¶</a></dt> |
| 1741 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1742 | <col class="field-name" /> |
| 1743 | <col class="field-body" /> |
| 1744 | <tbody valign="top"> |
| 1745 | <tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">CI_Form_validation instance (method chaining)</td> |
| 1746 | </tr> |
| 1747 | <tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">CI_Form_validation</td> |
| 1748 | </tr> |
| 1749 | </tbody> |
| 1750 | </table> |
| 1751 | <p>Permits you to reset the validation when you validate more than one array. |
| 1752 | This method should be called before validating each new array.</p> |
| 1753 | </dd></dl> |
| 1754 | |
| 1755 | <dl class="method"> |
| 1756 | <dt id="CI_Form_validation::error_array"> |
| 1757 | <code class="descname">error_array</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::error_array" title="Permalink to this definition">¶</a></dt> |
| 1758 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1759 | <col class="field-name" /> |
| 1760 | <col class="field-body" /> |
| 1761 | <tbody valign="top"> |
| 1762 | <tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">Array of error messages</td> |
| 1763 | </tr> |
| 1764 | <tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">array</td> |
| 1765 | </tr> |
| 1766 | </tbody> |
| 1767 | </table> |
| 1768 | <p>Returns the error messages as an array.</p> |
| 1769 | </dd></dl> |
| 1770 | |
| 1771 | <dl class="method"> |
| 1772 | <dt id="CI_Form_validation::error_string"> |
| 1773 | <code class="descname">error_string</code><span class="sig-paren">(</span><span class="optional">[</span><em>$prefix = ''</em><span class="optional">[</span>, <em>$suffix = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::error_string" title="Permalink to this definition">¶</a></dt> |
| 1774 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1775 | <col class="field-name" /> |
| 1776 | <col class="field-body" /> |
| 1777 | <tbody valign="top"> |
| 1778 | <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> |
| 1779 | <li><strong>$prefix</strong> (<em>string</em>) – Error message prefix</li> |
| 1780 | <li><strong>$suffix</strong> (<em>string</em>) – Error message suffix</li> |
| 1781 | </ul> |
| 1782 | </td> |
| 1783 | </tr> |
| 1784 | <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">Error messages as a string</p> |
| 1785 | </td> |
| 1786 | </tr> |
| 1787 | <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p> |
| 1788 | </td> |
| 1789 | </tr> |
| 1790 | </tbody> |
| 1791 | </table> |
| 1792 | <p>Returns all error messages (as returned from error_array()) formatted as a |
| 1793 | string and separated by a newline character.</p> |
| 1794 | </dd></dl> |
| 1795 | |
| 1796 | <dl class="method"> |
| 1797 | <dt id="CI_Form_validation::error"> |
| 1798 | <code class="descname">error</code><span class="sig-paren">(</span><em>$field</em><span class="optional">[</span>, <em>$prefix = ''</em><span class="optional">[</span>, <em>$suffix = ''</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::error" title="Permalink to this definition">¶</a></dt> |
| 1799 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1800 | <col class="field-name" /> |
| 1801 | <col class="field-body" /> |
| 1802 | <tbody valign="top"> |
| 1803 | <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> |
| 1804 | <li><strong>$field</strong> (<em>string</em>) – Field name</li> |
| 1805 | <li><strong>$prefix</strong> (<em>string</em>) – Optional prefix</li> |
| 1806 | <li><strong>$suffix</strong> (<em>string</em>) – Optional suffix</li> |
| 1807 | </ul> |
| 1808 | </td> |
| 1809 | </tr> |
| 1810 | <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">Error message string</p> |
| 1811 | </td> |
| 1812 | </tr> |
| 1813 | <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p> |
| 1814 | </td> |
| 1815 | </tr> |
| 1816 | </tbody> |
| 1817 | </table> |
| 1818 | <p>Returns the error message for a specific field, optionally adding a |
| 1819 | prefix and/or suffix to it (usually HTML tags).</p> |
| 1820 | </dd></dl> |
| 1821 | |
| 1822 | <dl class="method"> |
| 1823 | <dt id="CI_Form_validation::has_rule"> |
| 1824 | <code class="descname">has_rule</code><span class="sig-paren">(</span><em>$field</em><span class="sig-paren">)</span><a class="headerlink" href="#CI_Form_validation::has_rule" title="Permalink to this definition">¶</a></dt> |
| 1825 | <dd><table class="docutils field-list" frame="void" rules="none"> |
| 1826 | <col class="field-name" /> |
| 1827 | <col class="field-body" /> |
| 1828 | <tbody valign="top"> |
| 1829 | <tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> |
| 1830 | <li><strong>$field</strong> (<em>string</em>) – Field name</li> |
| 1831 | </ul> |
| 1832 | </td> |
| 1833 | </tr> |
| 1834 | <tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">TRUE if the field has rules set, FALSE if not</p> |
| 1835 | </td> |
| 1836 | </tr> |
| 1837 | <tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">bool</p> |
| 1838 | </td> |
| 1839 | </tr> |
| 1840 | </tbody> |
| 1841 | </table> |
| 1842 | <p>Checks to see if there is a rule set for the specified field.</p> |
| 1843 | </dd></dl> |
| 1844 | |
| 1845 | </dd></dl> |
| 1846 | |
| 1847 | </div> |
| 1848 | <div class="section" id="helper-reference"> |
| 1849 | <span id="helper-functions"></span><h2><a class="toc-backref" href="#id35">Helper Reference</a><a class="headerlink" href="#helper-reference" title="Permalink to this headline">¶</a></h2> |
| 1850 | <p>Please refer to the <a class="reference internal" href="../helpers/form_helper.html"><span class="doc">Form Helper</span></a> manual for |
| 1851 | the following functions:</p> |
| 1852 | <ul class="simple"> |
| 1853 | <li><a class="reference internal" href="../helpers/form_helper.html#form_error" title="form_error"><code class="xref php php-func docutils literal"><span class="pre">form_error()</span></code></a></li> |
| 1854 | <li><a class="reference internal" href="../helpers/form_helper.html#validation_errors" title="validation_errors"><code class="xref php php-func docutils literal"><span class="pre">validation_errors()</span></code></a></li> |
| 1855 | <li><a class="reference internal" href="../helpers/form_helper.html#set_value" title="set_value"><code class="xref php php-func docutils literal"><span class="pre">set_value()</span></code></a></li> |
| 1856 | <li><a class="reference internal" href="../helpers/form_helper.html#set_select" title="set_select"><code class="xref php php-func docutils literal"><span class="pre">set_select()</span></code></a></li> |
| 1857 | <li><a class="reference internal" href="../helpers/form_helper.html#set_checkbox" title="set_checkbox"><code class="xref php php-func docutils literal"><span class="pre">set_checkbox()</span></code></a></li> |
| 1858 | <li><a class="reference internal" href="../helpers/form_helper.html#set_radio" title="set_radio"><code class="xref php php-func docutils literal"><span class="pre">set_radio()</span></code></a></li> |
| 1859 | </ul> |
| 1860 | <p>Note that these are procedural functions, so they <strong>do not</strong> require you |
| 1861 | to prepend them with <code class="docutils literal"><span class="pre">$this->form_validation</span></code>.</p> |
| 1862 | </div> |
| 1863 | </div> |
| 1864 | |
| 1865 | |
| 1866 | </div> |
| 1867 | <footer> |
| 1868 | |
| 1869 | <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> |
| 1870 | |
| 1871 | <a href="ftp.html" class="btn btn-neutral float-right" title="FTP Class">Next <span class="fa fa-arrow-circle-right"></span></a> |
| 1872 | |
| 1873 | |
| 1874 | <a href="file_uploading.html" class="btn btn-neutral" title="File Uploading Class"><span class="fa fa-arrow-circle-left"></span> Previous</a> |
| 1875 | |
| 1876 | </div> |
| 1877 | |
| 1878 | |
| 1879 | <hr/> |
| 1880 | |
| 1881 | <div role="contentinfo"> |
| 1882 | <p> |
| 1883 | © Copyright 2014 - 2019, British Columbia Institute of Technology. |
| 1884 | Last updated on Jan 16, 2019. |
| 1885 | </p> |
| 1886 | </div> |
| 1887 | |
| 1888 | Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. |
| 1889 | |
| 1890 | </footer> |
| 1891 | </div> |
| 1892 | </div> |
| 1893 | |
| 1894 | </section> |
| 1895 | |
| 1896 | </div> |
| 1897 | |
| 1898 | |
| 1899 | |
| 1900 | |
| 1901 | |
| 1902 | <script type="text/javascript"> |
| 1903 | var DOCUMENTATION_OPTIONS = { |
| 1904 | URL_ROOT:'../', |
| 1905 | VERSION:'3.1.10', |
| 1906 | COLLAPSE_INDEX:false, |
| 1907 | FILE_SUFFIX:'.html', |
| 1908 | HAS_SOURCE: false |
| 1909 | }; |
| 1910 | </script> |
| 1911 | <script type="text/javascript" src="../_static/jquery.js"></script> |
| 1912 | <script type="text/javascript" src="../_static/underscore.js"></script> |
| 1913 | <script type="text/javascript" src="../_static/doctools.js"></script> |
| 1914 | |
| 1915 | |
| 1916 | |
| 1917 | |
| 1918 | |
| 1919 | <script type="text/javascript" src="../_static/js/theme.js"></script> |
| 1920 | |
| 1921 | |
| 1922 | |
| 1923 | |
| 1924 | <script type="text/javascript"> |
| 1925 | jQuery(function () { |
| 1926 | SphinxRtdTheme.StickyNav.enable(); |
| 1927 | }); |
| 1928 | </script> |
| 1929 | |
| 1930 | |
| 1931 | </body> |
| 1932 | </html> |