diff options
author | Andrey Andreev <narf@devilix.net> | 2018-01-13 12:57:11 +0100 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2018-01-13 12:57:11 +0100 |
commit | 02a0518e1c56ba3506f370362ac5ebdb8a138312 (patch) | |
tree | 8b4684192b403dcae17d298eb4fc9e7ab001acad /user_guide/libraries/form_validation.html | |
parent | f0b9735d5857e6c07c8f5824b13cff4828db3cfe (diff) |
[ci skip] 3.1.7 release
Diffstat (limited to 'user_guide/libraries/form_validation.html')
-rw-r--r-- | user_guide/libraries/form_validation.html | 1932 |
1 files changed, 1932 insertions, 0 deletions
diff --git a/user_guide/libraries/form_validation.html b/user_guide/libraries/form_validation.html new file mode 100644 index 000000000..3d9da783b --- /dev/null +++ b/user_guide/libraries/form_validation.html @@ -0,0 +1,1932 @@ + + +<!DOCTYPE html> +<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]--> +<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]--> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + + <title>Form Validation — CodeIgniter 3.1.7 documentation</title> + + + + + <link rel="shortcut icon" href="../_static/ci-icon.ico"/> + + + + <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'> + + + + + + + + + + <link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" /> + + + + <link rel="index" title="Index" + href="../genindex.html"/> + <link rel="search" title="Search" href="../search.html"/> + <link rel="top" title="CodeIgniter 3.1.7 documentation" href="../index.html"/> + <link rel="up" title="Libraries" href="index.html"/> + <link rel="next" title="FTP Class" href="ftp.html"/> + <link rel="prev" title="File Uploading Class" href="file_uploading.html"/> + + + <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script> + +</head> + +<body class="wy-body-for-nav" role="document"> + + <div id="nav"> + <div id="nav_inner"> + + + + <div id="pulldown-menu" class="ciNav"> + <ul> +<li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">Welcome to CodeIgniter</a></li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li> +<li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li> +<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li> +<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li> +<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li> +<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing to CodeIgniter</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li> +<li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">Controllers</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">Reserved Names</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/views.html">Views</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/models.html">Models</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helpers</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">Using CodeIgniter Libraries</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">Creating Libraries</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">Using CodeIgniter Drivers</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">Creating Drivers</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">Creating Core System Classes</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">Creating Ancillary Classes</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">Hooks - Extending the Framework Core</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">Auto-loading Resources</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Common Functions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">Compatibility Functions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI Routing</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Caching</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">Profiling Your Application</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/cli.html">Running via the CLI</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">Alternate PHP Syntax for View Files</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/security.html">Security</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP Style Guide</a></li> +</ul> +</li> +</ul> +<ul class="current"> +<li class="toctree-l1 current"><a class="reference internal" href="index.html">Libraries</a><ul class="current"> +<li class="toctree-l2"><a class="reference internal" href="benchmark.html">Benchmarking Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li> +<li class="toctree-l2"><a class="reference internal" href="calendar.html">Calendaring Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="cart.html">Shopping Cart Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="config.html">Config Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="encrypt.html">Encrypt Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="encryption.html">Encryption Library</a></li> +<li class="toctree-l2"><a class="reference internal" href="file_uploading.html">File Uploading Class</a></li> +<li class="toctree-l2 current"><a class="current reference internal" href="#">Form Validation</a></li> +<li class="toctree-l2"><a class="reference internal" href="ftp.html">FTP Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="image_lib.html">Image Manipulation Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="input.html">Input Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="javascript.html">Javascript Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="language.html">Language Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="loader.html">Loader Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="migration.html">Migrations Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="output.html">Output Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="parser.html">Template Parser Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="security.html">Security Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="sessions.html">Session Library</a></li> +<li class="toctree-l2"><a class="reference internal" href="table.html">HTML Table Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="trackback.html">Trackback Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="typography.html">Typography Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="unit_testing.html">Unit Testing Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="uri.html">URI Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li> +<li class="toctree-l2"><a class="reference internal" href="zip.html">Zip Encoding Class</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Database Reference</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/caching.html">Query Caching</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/forge.html">Database Manipulation with Database Forge</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/db_driver_reference.html">Database Driver Reference</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">CAPTCHA Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">Directory Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">Language Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">Path Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">Smiley Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">Typography Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li> +</ul> +</li> +</ul> + + </div> + + + </div> +</div> +<div id="nav2"> + <a href="#" id="openToc"> + <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" /> + </a> +</div> + + <div class="wy-grid-for-nav"> + + + <nav data-toggle="wy-nav-shift" class="wy-nav-side"> + <div class="wy-side-nav-search"> + + <a href="../index.html" class="fa fa-home"> CodeIgniter</a> + + +<div role="search"> + <form id="rtd-search-form" class="wy-form" action="../search.html" method="get"> + <input type="text" name="q" placeholder="Search docs" /> + <input type="hidden" name="check_keywords" value="yes" /> + <input type="hidden" name="area" value="default" /> + </form> +</div> + </div> + + <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation"> + + + + <ul> +<li class="toctree-l1"><a class="reference internal" href="../general/welcome.html">Welcome to CodeIgniter</a></li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation Instructions</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../installation/downloads.html">Downloading CodeIgniter</a></li> +<li class="toctree-l2"><a class="reference internal" href="../installation/index.html">Installation Instructions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li> +<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../overview/index.html">CodeIgniter Overview</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../overview/getting_started.html">Getting Started</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/at_a_glance.html">CodeIgniter at a Glance</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/features.html">Supported Features</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/appflow.html">Application Flow Chart</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/mvc.html">Model-View-Controller</a></li> +<li class="toctree-l2"><a class="reference internal" href="../overview/goals.html">Architectural Goals</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li> +<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li> +<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li> +<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../contributing/index.html">Contributing to CodeIgniter</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../documentation/index.html">Writing CodeIgniter Documentation</a></li> +<li class="toctree-l2"><a class="reference internal" href="../DCO.html">Developer’s Certificate of Origin 1.1</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/controllers.html">Controllers</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/reserved_names.html">Reserved Names</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/views.html">Views</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/models.html">Models</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helpers</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/libraries.html">Using CodeIgniter Libraries</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/creating_libraries.html">Creating Libraries</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/drivers.html">Using CodeIgniter Drivers</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/creating_drivers.html">Creating Drivers</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/core_classes.html">Creating Core System Classes</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/ancillary_classes.html">Creating Ancillary Classes</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/hooks.html">Hooks - Extending the Framework Core</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/autoloader.html">Auto-loading Resources</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Common Functions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/compatibility_functions.html">Compatibility Functions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/routing.html">URI Routing</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Caching</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/profiling.html">Profiling Your Application</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/cli.html">Running via the CLI</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/alternative_php.html">Alternate PHP Syntax for View Files</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/security.html">Security</a></li> +<li class="toctree-l2"><a class="reference internal" href="../general/styleguide.html">PHP Style Guide</a></li> +</ul> +</li> +</ul> +<ul class="current"> +<li class="toctree-l1 current"><a class="reference internal" href="index.html">Libraries</a><ul class="current"> +<li class="toctree-l2"><a class="reference internal" href="benchmark.html">Benchmarking Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li> +<li class="toctree-l2"><a class="reference internal" href="calendar.html">Calendaring Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="cart.html">Shopping Cart Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="config.html">Config Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="encrypt.html">Encrypt Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="encryption.html">Encryption Library</a></li> +<li class="toctree-l2"><a class="reference internal" href="file_uploading.html">File Uploading Class</a></li> +<li class="toctree-l2 current"><a class="current reference internal" href="#">Form Validation</a></li> +<li class="toctree-l2"><a class="reference internal" href="ftp.html">FTP Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="image_lib.html">Image Manipulation Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="input.html">Input Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="javascript.html">Javascript Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="language.html">Language Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="loader.html">Loader Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="migration.html">Migrations Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="output.html">Output Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="parser.html">Template Parser Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="security.html">Security Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="sessions.html">Session Library</a></li> +<li class="toctree-l2"><a class="reference internal" href="table.html">HTML Table Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="trackback.html">Trackback Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="typography.html">Typography Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="unit_testing.html">Unit Testing Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="uri.html">URI Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="xmlrpc.html">XML-RPC and XML-RPC Server Classes</a></li> +<li class="toctree-l2"><a class="reference internal" href="zip.html">Zip Encoding Class</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Database Reference</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/caching.html">Query Caching</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/forge.html">Database Manipulation with Database Forge</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities Class</a></li> +<li class="toctree-l2"><a class="reference internal" href="../database/db_driver_reference.html">Database Driver Reference</a></li> +</ul> +</li> +</ul> +<ul> +<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul> +<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/captcha_helper.html">CAPTCHA Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/directory_helper.html">Directory Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/download_helper.html">Download Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/email_helper.html">Email Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/file_helper.html">File Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/language_helper.html">Language Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/path_helper.html">Path Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/smiley_helper.html">Smiley Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/string_helper.html">String Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/typography_helper.html">Typography Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li> +<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li> +</ul> +</li> +</ul> + + + + </div> + + </nav> + + <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"> + + + <nav class="wy-nav-top" role="navigation" aria-label="top navigation"> + <i data-toggle="wy-nav-top" class="fa fa-bars"></i> + <a href="../index.html">CodeIgniter</a> + </nav> + + + + <div class="wy-nav-content"> + <div class="rst-content"> + <div role="navigation" aria-label="breadcrumbs navigation"> + <ul class="wy-breadcrumbs"> + <li><a href="../index.html">Docs</a> »</li> + + <li><a href="index.html">Libraries</a> »</li> + + <li>Form Validation</li> + <li class="wy-breadcrumbs-aside"> + + </li> + <div style="float:right;margin-left:5px;" id="closeMe"> + <img title="Classic Layout" alt="classic layout" src="data:image/gif;base64,R0lGODlhFAAUAJEAAAAAADMzM////wAAACH5BAUUAAIALAAAAAAUABQAAAImlI+py+0PU5gRBRDM3DxbWoXis42X13USOLauUIqnlsaH/eY6UwAAOw==" /> + </div> + </ul> + <hr/> +</div> + <div role="main" class="document"> + + <div class="section" id="form-validation"> +<h1><a class="toc-backref" href="#id6">Form Validation</a><a class="headerlink" href="#form-validation" title="Permalink to this headline">¶</a></h1> +<p>CodeIgniter provides a comprehensive form validation and data prepping +class that helps minimize the amount of code you’ll write.</p> +<div class="contents topic" id="page-contents"> +<p class="topic-title first">Page Contents</p> +<ul class="simple"> +<li><a class="reference internal" href="#form-validation" id="id6">Form Validation</a><ul> +<li><a class="reference internal" href="#overview" id="id7">Overview</a></li> +<li><a class="reference internal" href="#form-validation-tutorial" id="id8">Form Validation Tutorial</a><ul> +<li><a class="reference internal" href="#the-form" id="id9">The Form</a></li> +<li><a class="reference internal" href="#the-success-page" id="id10">The Success Page</a></li> +<li><a class="reference internal" href="#the-controller" id="id11">The Controller</a></li> +<li><a class="reference internal" href="#try-it" id="id12">Try it!</a></li> +<li><a class="reference internal" href="#explanation" id="id13">Explanation</a></li> +<li><a class="reference internal" href="#setting-validation-rules" id="id14">Setting Validation Rules</a></li> +<li><a class="reference internal" href="#setting-rules-using-an-array" id="id15">Setting Rules Using an Array</a></li> +<li><a class="reference internal" href="#cascading-rules" id="id16">Cascading Rules</a></li> +<li><a class="reference internal" href="#prepping-data" id="id17">Prepping Data</a></li> +<li><a class="reference internal" href="#re-populating-the-form" id="id18">Re-populating the form</a></li> +<li><a class="reference internal" href="#callbacks-your-own-validation-methods" id="id19">Callbacks: Your own Validation Methods</a></li> +<li><a class="reference internal" href="#callable-use-anything-as-a-rule" id="id20">Callable: Use anything as a rule</a></li> +<li><a class="reference internal" href="#setting-error-messages" id="id21">Setting Error Messages</a></li> +<li><a class="reference internal" href="#translating-field-names" id="id22">Translating Field Names</a></li> +<li><a class="reference internal" href="#changing-the-error-delimiters" id="id23">Changing the Error Delimiters</a></li> +<li><a class="reference internal" href="#showing-errors-individually" id="id24">Showing Errors Individually</a></li> +<li><a class="reference internal" href="#validating-an-array-other-than-post" id="id25">Validating an Array (other than $_POST)</a></li> +</ul> +</li> +<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> +<li><a class="reference internal" href="#how-to-save-your-rules" id="id27">How to save your rules</a></li> +<li><a class="reference internal" href="#creating-sets-of-rules" id="id28">Creating Sets of Rules</a></li> +<li><a class="reference internal" href="#calling-a-specific-rule-group" id="id29">Calling a Specific Rule Group</a></li> +<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> +</ul> +</li> +<li><a class="reference internal" href="#using-arrays-as-field-names" id="id31">Using Arrays as Field Names</a></li> +<li><a class="reference internal" href="#rule-reference" id="id32">Rule Reference</a></li> +<li><a class="reference internal" href="#prepping-reference" id="id33">Prepping Reference</a></li> +<li><a class="reference internal" href="#class-reference" id="id34">Class Reference</a></li> +<li><a class="reference internal" href="#helper-reference" id="id35">Helper Reference</a></li> +</ul> +</li> +</ul> +</div> +<div class="section" id="overview"> +<h2><a class="toc-backref" href="#id7">Overview</a><a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2> +<p>Before explaining CodeIgniter’s approach to data validation, let’s +describe the ideal scenario:</p> +<ol class="arabic simple"> +<li>A form is displayed.</li> +<li>You fill it in and submit it.</li> +<li>If you submitted something invalid, or perhaps missed a required +item, the form is redisplayed containing your data along with an +error message describing the problem.</li> +<li>This process continues until you have submitted a valid form.</li> +</ol> +<p>On the receiving end, the script must:</p> +<ol class="arabic simple"> +<li>Check for required data.</li> +<li>Verify that the data is of the correct type, and meets the correct +criteria. For example, if a username is submitted it must be +validated to contain only permitted characters. It must be of a +minimum length, and not exceed a maximum length. The username can’t +be someone else’s existing username, or perhaps even a reserved word. +Etc.</li> +<li>Sanitize the data for security.</li> +<li>Pre-format the data if needed (Does the data need to be trimmed? HTML +encoded? Etc.)</li> +<li>Prep the data for insertion in the database.</li> +</ol> +<p>Although there is nothing terribly complex about the above process, it +usually requires a significant amount of code, and to display error +messages, various control structures are usually placed within the form +HTML. Form validation, while simple to create, is generally very messy +and tedious to implement.</p> +</div> +<div class="section" id="form-validation-tutorial"> +<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> +<p>What follows is a “hands on” tutorial for implementing CodeIgniters Form +Validation.</p> +<p>In order to implement form validation you’ll need three things:</p> +<ol class="arabic simple"> +<li>A <a class="reference internal" href="../general/views.html"><span class="doc">View</span></a> file containing a form.</li> +<li>A View file containing a “success” message to be displayed upon +successful submission.</li> +<li>A <a class="reference internal" href="../general/controllers.html"><span class="doc">controller</span></a> method to receive and +process the submitted data.</li> +</ol> +<p>Let’s create those three things, using a member sign-up form as the +example.</p> +<div class="section" id="the-form"> +<h3><a class="toc-backref" href="#id9">The Form</a><a class="headerlink" href="#the-form" title="Permalink to this headline">¶</a></h3> +<p>Using a text editor, create a form called myform.php. In it, place this +code and save it to your application/views/ folder:</p> +<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">html</span><span class="o">></span> +<span class="o"><</span><span class="nx">head</span><span class="o">></span> +<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> +<span class="o"></</span><span class="nx">head</span><span class="o">></span> +<span class="o"><</span><span class="nx">body</span><span class="o">></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> + +<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> + +<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> +<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> + +<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> +<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> + +<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> +<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> + +<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> +<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> + +<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> + +<span class="p"></</span><span class="nt">form</span><span class="p">></span> + +<span class="p"></</span><span class="nt">body</span><span class="p">></span> +<span class="p"></</span><span class="nt">html</span><span class="p">></span> +</pre></div> +</div> +</div> +<div class="section" id="the-success-page"> +<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> +<p>Using a text editor, create a form called formsuccess.php. In it, place +this code and save it to your application/views/ folder:</p> +<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">html</span><span class="o">></span> +<span class="o"><</span><span class="nx">head</span><span class="o">></span> +<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> +<span class="o"></</span><span class="nx">head</span><span class="o">></span> +<span class="o"><</span><span class="nx">body</span><span class="o">></span> + +<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> + +<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> + +<span class="p"></</span><span class="nt">body</span><span class="p">></span> +<span class="p"></</span><span class="nt">html</span><span class="p">></span> +</pre></div> +</div> +</div> +<div class="section" id="the-controller"> +<h3><a class="toc-backref" href="#id11">The Controller</a><a class="headerlink" href="#the-controller" title="Permalink to this headline">¶</a></h3> +<p>Using a text editor, create a controller called Form.php. In it, place +this code and save it to your application/controllers/ folder:</p> +<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> + +<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> + + <span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span> + <span class="p">{</span> + <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> + + <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> + + <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> + <span class="p">{</span> + <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> + <span class="p">}</span> + <span class="k">else</span> + <span class="p">{</span> + <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> + <span class="p">}</span> + <span class="p">}</span> +<span class="p">}</span> +</pre></div> +</div> +</div> +<div class="section" id="try-it"> +<h3><a class="toc-backref" href="#id12">Try it!</a><a class="headerlink" href="#try-it" title="Permalink to this headline">¶</a></h3> +<p>To try your form, visit your site using a URL similar to this one:</p> +<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> +</pre></div> +</div> +<p>If you submit the form you should simply see the form reload. That’s +because you haven’t set up any validation rules yet.</p> +<p><strong>Since you haven’t told the Form Validation class to validate anything +yet, it returns FALSE (boolean false) by default. ``The run()`` method +only returns TRUE if it has successfully applied your rules without any +of them failing.</strong></p> +</div> +<div class="section" id="explanation"> +<h3><a class="toc-backref" href="#id13">Explanation</a><a class="headerlink" href="#explanation" title="Permalink to this headline">¶</a></h3> +<p>You’ll notice several things about the above pages:</p> +<p>The form (myform.php) is a standard web form with a couple exceptions:</p> +<ol class="arabic"> +<li><p class="first">It uses a form helper to create the form opening. Technically, this +isn’t necessary. You could create the form using standard HTML. +However, the benefit of using the helper is that it generates the +action URL for you, based on the URL in your config file. This makes +your application more portable in the event your URLs change.</p> +</li> +<li><p class="first">At the top of the form you’ll notice the following function call:</p> +<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> +</pre></div> +</div> +<p>This function will return any error messages sent back by the +validator. If there are no messages it returns an empty string.</p> +</li> +</ol> +<p>The controller (Form.php) has one method: <code class="docutils literal"><span class="pre">index()</span></code>. This method +initializes the validation class and loads the form helper and URL +helper used by your view files. It also runs the validation routine. +Based on whether the validation was successful it either presents the +form or the success page.</p> +</div> +<div class="section" id="setting-validation-rules"> +<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> +<p>CodeIgniter lets you set as many validation rules as you need for a +given field, cascading them in order, and it even lets you prep and +pre-process the field data at the same time. To set validation rules you +will use the <code class="docutils literal"><span class="pre">set_rules()</span></code> method:</p> +<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> +</pre></div> +</div> +<p>The above method takes <strong>three</strong> parameters as input:</p> +<ol class="arabic simple"> +<li>The field name - the exact name you’ve given the form field.</li> +<li>A “human” name for this field, which will be inserted into the error +message. For example, if your field is named “user” you might give it +a human name of “Username”.</li> +<li>The validation rules for this form field.</li> +<li>(optional) Set custom error messages on any rules given for current field. If not provided will use the default one.</li> +</ol> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p class="last">If you would like the field name to be stored in a language +file, please see <a class="reference internal" href="#translating-field-names"><span class="std std-ref">Translating Field Names</span></a>.</p> +</div> +<p>Here is an example. In your controller (Form.php), add this code just +below the validation initialization method:</p> +<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> +<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> +<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> +<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> +</pre></div> +</div> +<p>Your controller should now look like this:</p> +<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> + +<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> + + <span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span> + <span class="p">{</span> + <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> + + <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> + + <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> + <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> + <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> + <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">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> + <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> + + <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> + <span class="p">{</span> + <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> + <span class="p">}</span> + <span class="k">else</span> + <span class="p">{</span> + <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> + <span class="p">}</span> + <span class="p">}</span> +<span class="p">}</span> +</pre></div> +</div> +<p>Now submit the form with the fields blank and you should see the error +messages. If you submit the form with all the fields populated you’ll +see your success page.</p> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p class="last">The form fields are not yet being re-populated with the data +when there is an error. We’ll get to that shortly.</p> +</div> +</div> +<div class="section" id="setting-rules-using-an-array"> +<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> +<p>Before moving on it should be noted that the rule setting method can +be passed an array if you prefer to set all your rules in one action. If +you use this approach, you must name your array keys as indicated:</p> +<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> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'username'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Username'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'password'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span><span class="p">,</span> + <span class="s1">'errors'</span> <span class="o">=></span> <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> + <span class="p">),</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'passconf'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'email'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Email'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">)</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">set_rules</span><span class="p">(</span><span class="nv">$config</span><span class="p">);</span> +</pre></div> +</div> +</div> +<div class="section" id="cascading-rules"> +<h3><a class="toc-backref" href="#id16">Cascading Rules</a><a class="headerlink" href="#cascading-rules" title="Permalink to this headline">¶</a></h3> +<p>CodeIgniter lets you pipe multiple rules together. Let’s try it. Change +your rules in the third parameter of rule setting method, like this:</p> +<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|min_length[5]|max_length[12]|is_unique[users.username]'</span><span class="p">,</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'required'</span> <span class="o">=></span> <span class="s1">'You have not provided %s.'</span><span class="p">,</span> + <span class="s1">'is_unique'</span> <span class="o">=></span> <span class="s1">'This %s already exists.'</span> + <span class="p">)</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">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> +<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> +<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> +</pre></div> +</div> +<p>The above code sets the following rules:</p> +<ol class="arabic simple"> +<li>The username field be no shorter than 5 characters and no longer than +12.</li> +<li>The password field must match the password confirmation field.</li> +<li>The email field must contain a valid email address.</li> +</ol> +<p>Give it a try! Submit your form without the proper data and you’ll see +new error messages that correspond to your new rules. There are numerous +rules available which you can read about in the validation reference.</p> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p>You can also pass an array of rules to <code class="docutils literal"><span class="pre">set_rules()</span></code>, +instead of a string. Example:</p> +<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> +</pre></div> +</div> +</div> +</div> +<div class="section" id="prepping-data"> +<h3><a class="toc-backref" href="#id17">Prepping Data</a><a class="headerlink" href="#prepping-data" title="Permalink to this headline">¶</a></h3> +<p>In addition to the validation method like the ones we used above, you +can also prep your data in various ways. For example, you can set up +rules like this:</p> +<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> +<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> +<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> +<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> +</pre></div> +</div> +<p>In the above example, we are “trimming” the fields, checking for length +where necessary and making sure that both password fields match.</p> +<p><strong>Any native PHP function that accepts one parameter can be used as a +rule, like ``htmlspecialchars()``, ``trim()``, etc.</strong></p> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p class="last">You will generally want to use the prepping functions +<strong>after</strong> the validation rules so if there is an error, the +original data will be shown in the form.</p> +</div> +</div> +<div class="section" id="re-populating-the-form"> +<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> +<p>Thus far we have only been dealing with errors. It’s time to repopulate +the form field with the submitted data. CodeIgniter offers several +helper functions that permit you to do this. The one you will use most +commonly is:</p> +<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> +</pre></div> +</div> +<p>Open your myform.php view file and update the <strong>value</strong> in each field +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> +<p><strong>Don’t forget to include each field name in the :php:func:`set_value()` +function calls!</strong></p> +<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><</span><span class="nx">html</span><span class="o">></span> +<span class="o"><</span><span class="nx">head</span><span class="o">></span> +<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> +<span class="o"></</span><span class="nx">head</span><span class="o">></span> +<span class="o"><</span><span class="nx">body</span><span class="o">></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> + +<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> + +<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> +<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> + +<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> +<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> + +<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> +<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> + +<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> +<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> + +<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> + +<span class="p"></</span><span class="nt">form</span><span class="p">></span> + +<span class="p"></</span><span class="nt">body</span><span class="p">></span> +<span class="p"></</span><span class="nt">html</span><span class="p">></span> +</pre></div> +</div> +<p>Now reload your page and submit the form so that it triggers an error. +Your form fields should now be re-populated</p> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p class="last">The <a class="reference internal" href="#class-reference"><span class="std std-ref">Class Reference</span></a> section below +contains methods that permit you to re-populate <select> menus, +radio buttons, and checkboxes.</p> +</div> +<div class="admonition important"> +<p class="first admonition-title">Important</p> +<p>If you use an array as the name of a form field, you +must supply it as an array to the function. Example:</p> +<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> +</pre></div> +</div> +</div> +<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> +</div> +<div class="section" id="callbacks-your-own-validation-methods"> +<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> +<p>The validation system supports callbacks to your own validation +methods. This permits you to extend the validation class to meet your +needs. For example, if you need to run a database query to see if the +user is choosing a unique username, you can create a callback method +that does that. Let’s create an example of this.</p> +<p>In your controller, change the “username” rule to this:</p> +<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> +</pre></div> +</div> +<p>Then add a new method called <code class="docutils literal"><span class="pre">username_check()</span></code> to your controller. +Here’s how your controller should now look:</p> +<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> + +<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> + + <span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span> + <span class="p">{</span> + <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> + + <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> + + <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> + <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> + <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> + <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> + + <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> + <span class="p">{</span> + <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> + <span class="p">}</span> + <span class="k">else</span> + <span class="p">{</span> + <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> + <span class="p">}</span> + <span class="p">}</span> + + <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> + <span class="p">{</span> + <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> + <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">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> + <span class="k">return</span> <span class="k">FALSE</span><span class="p">;</span> + <span class="p">}</span> + <span class="k">else</span> + <span class="p">{</span> + <span class="k">return</span> <span class="k">TRUE</span><span class="p">;</span> + <span class="p">}</span> + <span class="p">}</span> + +<span class="p">}</span> +</pre></div> +</div> +<p>Reload your form and submit it with the word “test” as the username. You +can see that the form field data was passed to your callback method +for you to process.</p> +<p>To invoke a callback just put the method name in a rule, with +“callback_” as the rule <strong>prefix</strong>. If you need to receive an extra +parameter in your callback method, just add it normally after the +method name between square brackets, as in: <code class="docutils literal"><span class="pre">callback_foo[bar]</span></code>, +then it will be passed as the second argument of your callback method.</p> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p class="last">You can also process the form data that is passed to your +callback and return it. If your callback returns anything other than a +boolean TRUE/FALSE it is assumed that the data is your newly processed +form data.</p> +</div> +</div> +<div class="section" id="callable-use-anything-as-a-rule"> +<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> +<p>If callback rules aren’t good enough for you (for example, because they are +limited to your controller), don’t get disappointed, there’s one more way +to create custom rules: anything that <code class="docutils literal"><span class="pre">is_callable()</span></code> would return TRUE for.</p> +<p>Consider the following example:</p> +<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="k">array</span><span class="p">(</span> + <span class="s1">'required'</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> + <span class="p">)</span> +<span class="p">);</span> +</pre></div> +</div> +<p>The above code would use the <code class="docutils literal"><span class="pre">valid_username()</span></code> method from your +<code class="docutils literal"><span class="pre">Users_model</span></code> object.</p> +<p>This is just an example of course, and callbacks aren’t limited to models. +You can use any object/method that accepts the field value as its’ first +parameter. You can also use an anonymous function:</p> +<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="k">array</span><span class="p">(</span> + <span class="s1">'required'</span><span class="p">,</span> + <span class="k">function</span><span class="p">(</span><span class="nv">$value</span><span class="p">)</span> + <span class="p">{</span> + <span class="c1">// Check $value</span> + <span class="p">}</span> + <span class="p">)</span> +<span class="p">);</span> +</pre></div> +</div> +<p>Of course, since a Callable rule by itself is not a string, it isn’t +a rule name either. That is a problem when you want to set error messages +for them. In order to get around that problem, you can put such rules as +the second element of an array, with the first one being the rule name:</p> +<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="k">array</span><span class="p">(</span> + <span class="s1">'required'</span><span class="p">,</span> + <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> + <span class="p">)</span> +<span class="p">);</span> +</pre></div> +</div> +<p>Anonymous function version:</p> +<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="k">array</span><span class="p">(</span> + <span class="s1">'required'</span><span class="p">,</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'username_callable'</span><span class="p">,</span> + <span class="k">function</span><span class="p">(</span><span class="nv">$str</span><span class="p">)</span> + <span class="p">{</span> + <span class="c1">// Check validity of $str and return TRUE or FALSE</span> + <span class="p">}</span> + <span class="p">)</span> + <span class="p">)</span> +<span class="p">);</span> +</pre></div> +</div> +</div> +<div class="section" id="setting-error-messages"> +<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> +<p>All of the native error messages are located in the following language +file: <strong>system/language/english/form_validation_lang.php</strong></p> +<p>To set your own global custom message for a rule, you can either +extend/override the language file by creating your own in +<strong>application/language/english/form_validation_lang.php</strong> (read more +about this in the <a class="reference internal" href="language.html"><span class="doc">Language Class</span></a> documentation), +or use the following method:</p> +<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> +</pre></div> +</div> +<p>If you need to set a custom error message for a particular field on +some particular rule, use the set_rules() method:</p> +<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> + <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> +<span class="p">);</span> +</pre></div> +</div> +<p>Where rule corresponds to the name of a particular rule, and Error +Message is the text you would like displayed.</p> +<p>If you’d like to include a field’s “human” name, or the optional +parameter some rules allow for (such as max_length), you can add the +<strong>{field}</strong> and <strong>{param}</strong> tags to your message, respectively:</p> +<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> +</pre></div> +</div> +<p>On a field with the human name Username and a rule of min_length[5], an +error would display: “Username must have at least 5 characters.”</p> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p class="last">The old <cite>sprintf()</cite> method of using <strong>%s</strong> in your error messages +will still work, however it will override the tags above. You should +use one or the other.</p> +</div> +<p>In the callback rule example above, the error message was set by passing +the name of the method (without the “callback_” prefix):</p> +<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> +</pre></div> +</div> +</div> +<div class="section" id="translating-field-names"> +<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> +<p>If you would like to store the “human” name you passed to the +<code class="docutils literal"><span class="pre">set_rules()</span></code> method in a language file, and therefore make the name +able to be translated, here’s how:</p> +<p>First, prefix your “human” name with <strong>lang:</strong>, as in this example:</p> +<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> +</pre></div> +</div> +<p>Then, store the name in one of your language file arrays (without the +prefix):</p> +<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> +</pre></div> +</div> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p>If you store your array item in a language file that is not +loaded automatically by CI, you’ll need to remember to load it in your +controller using:</p> +<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> +</pre></div> +</div> +</div> +<p>See the <a class="reference internal" href="language.html"><span class="doc">Language Class</span></a> page for more info regarding +language files.</p> +</div> +<div class="section" id="changing-the-error-delimiters"> +<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> +<p>By default, the Form Validation class adds a paragraph tag (<p>) around +each error message shown. You can either change these delimiters +globally, individually, or change the defaults in a config file.</p> +<ol class="arabic"> +<li><p class="first"><strong>Changing delimiters Globally</strong> +To globally change the error delimiters, in your controller method, +just after loading the Form Validation class, add this:</p> +<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> +</pre></div> +</div> +<p>In this example, we’ve switched to using div tags.</p> +</li> +<li><p class="first"><strong>Changing delimiters Individually</strong> +Each of the two error generating functions shown in this tutorial can +be supplied their own delimiters as follows:</p> +<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> +</pre></div> +</div> +<p>Or:</p> +<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> +</pre></div> +</div> +</li> +<li><p class="first"><strong>Set delimiters in a config file</strong> +You can add your error delimiters in application/config/form_validation.php as follows:</p> +<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> +<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> +</pre></div> +</div> +</li> +</ol> +</div> +<div class="section" id="showing-errors-individually"> +<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> +<p>If you prefer to show an error message next to each form field, rather +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> +<p>Try it! Change your form so that it looks like this:</p> +<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> +<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> +<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> + +<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> +<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> +<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> + +<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> +<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> +<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> + +<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> +<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> +<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> +</pre></div> +</div> +<p>If there are no errors, nothing will be shown. If there is an error, the +message will appear.</p> +<div class="admonition important"> +<p class="first admonition-title">Important</p> +<p>If you use an array as the name of a form field, you +must supply it as an array to the function. Example:</p> +<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> +<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> +</pre></div> +</div> +</div> +<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> +</div> +<div class="section" id="validating-an-array-other-than-post"> +<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> +<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> +<p>In this case, you can specify the array to be validated:</p> +<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> + <span class="s1">'username'</span> <span class="o">=></span> <span class="s1">'johndoe'</span><span class="p">,</span> + <span class="s1">'password'</span> <span class="o">=></span> <span class="s1">'mypassword'</span><span class="p">,</span> + <span class="s1">'passconf'</span> <span class="o">=></span> <span class="s1">'mypassword'</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">set_data</span><span class="p">(</span><span class="nv">$data</span><span class="p">);</span> +</pre></div> +</div> +<p>Creating validation rules, running the validation, and retrieving error +messages works the same whether you are validating <code class="docutils literal"><span class="pre">$_POST</span></code> data or +another array of your choice.</p> +<div class="admonition important"> +<p class="first admonition-title">Important</p> +<p class="last">You have to call the <code class="docutils literal"><span class="pre">set_data()</span></code> method <em>before</em> defining +any validation rules.</p> +</div> +<div class="admonition important"> +<p class="first admonition-title">Important</p> +<p class="last">If you want to validate more than one array during a single +execution, then you should call the <code class="docutils literal"><span class="pre">reset_validation()</span></code> method +before setting up rules and validating the new array.</p> +</div> +<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> +</div> +</div> +<div class="section" id="saving-sets-of-validation-rules-to-a-config-file"> +<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> +<p>A nice feature of the Form Validation class is that it permits you to +store all your validation rules for your entire application in a config +file. You can organize these rules into “groups”. These groups can +either be loaded automatically when a matching controller/method is +called, or you can manually call each set as needed.</p> +<div class="section" id="how-to-save-your-rules"> +<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> +<p>To store your validation rules, simply create a file named +form_validation.php in your application/config/ folder. In that file +you will place an array named $config with your rules. As shown earlier, +the validation array will have this prototype:</p> +<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> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'username'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Username'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'password'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'passconf'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'email'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Email'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">)</span> +<span class="p">);</span> +</pre></div> +</div> +<p>Your validation rule file will be loaded automatically and used when you +call the <code class="docutils literal"><span class="pre">run()</span></code> method.</p> +<p>Please note that you MUST name your <code class="docutils literal"><span class="pre">$config</span></code> array.</p> +</div> +<div class="section" id="creating-sets-of-rules"> +<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> +<p>In order to organize your rules into “sets” requires that you place them +into “sub arrays”. Consider the following example, showing two sets of +rules. We’ve arbitrarily called these two rules “signup” and “email”. +You can name your rules anything you want:</p> +<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> + <span class="s1">'signup'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'username'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Username'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'password'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'passconf'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password Confirmation'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'email'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Email'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">)</span> + <span class="p">),</span> + <span class="s1">'email'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'emailaddress'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'EmailAddress'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required|valid_email'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'name'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Name'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required|alpha'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'title'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Title'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'message'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'MessageBody'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">)</span> + <span class="p">)</span> +<span class="p">);</span> +</pre></div> +</div> +</div> +<div class="section" id="calling-a-specific-rule-group"> +<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> +<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> +method. For example, to call the signup rule you will do this:</p> +<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> +<span class="p">{</span> + <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> +<span class="p">}</span> +<span class="k">else</span> +<span class="p">{</span> + <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> +<span class="p">}</span> +</pre></div> +</div> +</div> +<div class="section" id="associating-a-controller-method-with-a-rule-group"> +<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> +<p>An alternate (and more automatic) method of calling a rule group is to +name it according to the controller class/method you intend to use it +with. For example, let’s say you have a controller named Member and a +method named signup. Here’s what your class might look like:</p> +<div class="highlight-ci"><div class="highlight"><pre><span></span><span class="o"><?</span><span class="nx">php</span> + +<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> + + <span class="k">public</span> <span class="k">function</span> <span class="nf">signup</span><span class="p">()</span> + <span class="p">{</span> + <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> + + <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> + <span class="p">{</span> + <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> + <span class="p">}</span> + <span class="k">else</span> + <span class="p">{</span> + <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> + <span class="p">}</span> + <span class="p">}</span> +<span class="p">}</span> +</pre></div> +</div> +<p>In your validation config file, you will name your rule group +member/signup:</p> +<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> + <span class="s1">'member/signup'</span> <span class="o">=></span> <span class="k">array</span><span class="p">(</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'username'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Username'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'password'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Password'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'passconf'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'PasswordConfirmation'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">),</span> + <span class="k">array</span><span class="p">(</span> + <span class="s1">'field'</span> <span class="o">=></span> <span class="s1">'email'</span><span class="p">,</span> + <span class="s1">'label'</span> <span class="o">=></span> <span class="s1">'Email'</span><span class="p">,</span> + <span class="s1">'rules'</span> <span class="o">=></span> <span class="s1">'required'</span> + <span class="p">)</span> + <span class="p">)</span> +<span class="p">);</span> +</pre></div> +</div> +<p>When a rule group is named identically to a controller class/method it +will be used automatically when the <code class="docutils literal"><span class="pre">run()</span></code> method is invoked from that +class/method.</p> +</div> +</div> +<div class="section" id="using-arrays-as-field-names"> +<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> +<p>The Form Validation class supports the use of arrays as field names. +Consider this example:</p> +<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> +</pre></div> +</div> +<p>If you do use an array as a field name, you must use the EXACT array +name in the <a class="reference internal" href="#helper-functions"><span class="std std-ref">Helper Functions</span></a> that require the +field name, and as your Validation Rule field name.</p> +<p>For example, to set a rule for the above field you would use:</p> +<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> +</pre></div> +</div> +<p>Or, to show an error for the above field you would use:</p> +<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> +</pre></div> +</div> +<p>Or to re-populate the field you would use:</p> +<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> +</pre></div> +</div> +<p>You can use multidimensional arrays as field names as well. For example:</p> +<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> +</pre></div> +</div> +<p>Or even:</p> +<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> +</pre></div> +</div> +<p>As with our first example, you must use the exact array name in the +helper functions:</p> +<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> +</pre></div> +</div> +<p>If you are using checkboxes (or other fields) that have multiple +options, don’t forget to leave an empty bracket after each option, so +that all selections will be added to the POST array:</p> +<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> +<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> +<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> +</pre></div> +</div> +<p>Or if you use a multidimensional array:</p> +<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> +<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> +<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> +</pre></div> +</div> +<p>When you use a helper function you’ll include the bracket as well:</p> +<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> +</pre></div> +</div> +</div> +<div class="section" id="rule-reference"> +<h2><a class="toc-backref" href="#id32">Rule Reference</a><a class="headerlink" href="#rule-reference" title="Permalink to this headline">¶</a></h2> +<p>The following is a list of all the native rules that are available to +use:</p> +<table border="1" class="docutils"> +<colgroup> +<col width="16%" /> +<col width="7%" /> +<col width="61%" /> +<col width="16%" /> +</colgroup> +<thead valign="bottom"> +<tr class="row-odd"><th class="head">Rule</th> +<th class="head">Parameter</th> +<th class="head">Description</th> +<th class="head">Example</th> +</tr> +</thead> +<tbody valign="top"> +<tr class="row-even"><td><strong>required</strong></td> +<td>No</td> +<td>Returns FALSE if the form element is empty.</td> +<td> </td> +</tr> +<tr class="row-odd"><td><strong>matches</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element does not match the one in the parameter.</td> +<td>matches[form_item]</td> +</tr> +<tr class="row-even"><td><strong>regex_match</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element does not match the regular expression.</td> +<td>regex_match[/regex/]</td> +</tr> +<tr class="row-odd"><td><strong>differs</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element does not differ from the one in the parameter.</td> +<td>differs[form_item]</td> +</tr> +<tr class="row-even"><td><strong>is_unique</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element is not unique to the table and field name in the +parameter. Note: This rule requires <a class="reference internal" href="../database/query_builder.html"><span class="doc">Query Builder</span></a> to be +enabled in order to work.</td> +<td>is_unique[table.field]</td> +</tr> +<tr class="row-odd"><td><strong>min_length</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element is shorter than the parameter value.</td> +<td>min_length[3]</td> +</tr> +<tr class="row-even"><td><strong>max_length</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element is longer than the parameter value.</td> +<td>max_length[12]</td> +</tr> +<tr class="row-odd"><td><strong>exact_length</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element is not exactly the parameter value.</td> +<td>exact_length[8]</td> +</tr> +<tr class="row-even"><td><strong>greater_than</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element is less than or equal to the parameter value or not +numeric.</td> +<td>greater_than[8]</td> +</tr> +<tr class="row-odd"><td><strong>greater_than_equal_to</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element is less than the parameter value, +or not numeric.</td> +<td>greater_than_equal_to[8]</td> +</tr> +<tr class="row-even"><td><strong>less_than</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element is greater than or equal to the parameter value or +not numeric.</td> +<td>less_than[8]</td> +</tr> +<tr class="row-odd"><td><strong>less_than_equal_to</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element is greater than the parameter value, +or not numeric.</td> +<td>less_than_equal_to[8]</td> +</tr> +<tr class="row-even"><td><strong>in_list</strong></td> +<td>Yes</td> +<td>Returns FALSE if the form element is not within a predetermined list.</td> +<td>in_list[red,blue,green]</td> +</tr> +<tr class="row-odd"><td><strong>alpha</strong></td> +<td>No</td> +<td>Returns FALSE if the form element contains anything other than alphabetical characters.</td> +<td> </td> +</tr> +<tr class="row-even"><td><strong>alpha_numeric</strong></td> +<td>No</td> +<td>Returns FALSE if the form element contains anything other than alpha-numeric characters.</td> +<td> </td> +</tr> +<tr class="row-odd"><td><strong>alpha_numeric_spaces</strong></td> +<td>No</td> +<td>Returns FALSE if the form element contains anything other than alpha-numeric characters +or spaces. Should be used after trim to avoid spaces at the beginning or end.</td> +<td> </td> +</tr> +<tr class="row-even"><td><strong>alpha_dash</strong></td> +<td>No</td> +<td>Returns FALSE if the form element contains anything other than alpha-numeric characters, +underscores or dashes.</td> +<td> </td> +</tr> +<tr class="row-odd"><td><strong>numeric</strong></td> +<td>No</td> +<td>Returns FALSE if the form element contains anything other than numeric characters.</td> +<td> </td> +</tr> +<tr class="row-even"><td><strong>integer</strong></td> +<td>No</td> +<td>Returns FALSE if the form element contains anything other than an integer.</td> +<td> </td> +</tr> +<tr class="row-odd"><td><strong>decimal</strong></td> +<td>No</td> +<td>Returns FALSE if the form element contains anything other than a decimal number.</td> +<td> </td> +</tr> +<tr class="row-even"><td><strong>is_natural</strong></td> +<td>No</td> +<td>Returns FALSE if the form element contains anything other than a natural number: +0, 1, 2, 3, etc.</td> +<td> </td> +</tr> +<tr class="row-odd"><td><strong>is_natural_no_zero</strong></td> +<td>No</td> +<td>Returns FALSE if the form element contains anything other than a natural +number, but not zero: 1, 2, 3, etc.</td> +<td> </td> +</tr> +<tr class="row-even"><td><strong>valid_url</strong></td> +<td>No</td> +<td>Returns FALSE if the form element does not contain a valid URL.</td> +<td> </td> +</tr> +<tr class="row-odd"><td><strong>valid_email</strong></td> +<td>No</td> +<td>Returns FALSE if the form element does not contain a valid email address.</td> +<td> </td> +</tr> +<tr class="row-even"><td><strong>valid_emails</strong></td> +<td>No</td> +<td>Returns FALSE if any value provided in a comma separated list is not a valid email.</td> +<td> </td> +</tr> +<tr class="row-odd"><td><strong>valid_ip</strong></td> +<td>Yes</td> +<td>Returns FALSE if the supplied IP address is not valid. +Accepts an optional parameter of ‘ipv4’ or ‘ipv6’ to specify an IP format.</td> +<td> </td> +</tr> +<tr class="row-even"><td><strong>valid_base64</strong></td> +<td>No</td> +<td>Returns FALSE if the supplied string contains anything other than valid Base64 characters.</td> +<td> </td> +</tr> +</tbody> +</table> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p>These rules can also be called as discrete methods. For +example:</p> +<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> +</pre></div> +</div> +</div> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p class="last">You can also use any native PHP functions that permit up +to two parameters, where at least one is required (to pass +the field data).</p> +</div> +</div> +<div class="section" id="prepping-reference"> +<h2><a class="toc-backref" href="#id33">Prepping Reference</a><a class="headerlink" href="#prepping-reference" title="Permalink to this headline">¶</a></h2> +<p>The following is a list of all the prepping methods that are available +to use:</p> +<table border="1" class="docutils"> +<colgroup> +<col width="14%" /> +<col width="6%" /> +<col width="79%" /> +</colgroup> +<thead valign="bottom"> +<tr class="row-odd"><th class="head">Name</th> +<th class="head">Parameter</th> +<th class="head">Description</th> +</tr> +</thead> +<tbody valign="top"> +<tr class="row-even"><td><strong>prep_for_form</strong></td> +<td>No</td> +<td>DEPRECATED: Converts special characters so that HTML data can be shown in a form field without breaking it.</td> +</tr> +<tr class="row-odd"><td><strong>prep_url</strong></td> +<td>No</td> +<td>Adds “http://” to URLs if missing.</td> +</tr> +<tr class="row-even"><td><strong>strip_image_tags</strong></td> +<td>No</td> +<td>Strips the HTML from image tags leaving the raw URL.</td> +</tr> +<tr class="row-odd"><td><strong>encode_php_tags</strong></td> +<td>No</td> +<td>Converts PHP tags to entities.</td> +</tr> +</tbody> +</table> +<div class="admonition note"> +<p class="first admonition-title">Note</p> +<p class="last">You can also use any native PHP functions that permits one +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>, +etc.</p> +</div> +</div> +<div class="section" id="class-reference"> +<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> +<dl class="class"> +<dt id="CI_Form_validation"> +<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> +<dd><dl class="method"> +<dt id="CI_Form_validation::set_rules"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> +<li><strong>$field</strong> (<em>string</em>) – Field name</li> +<li><strong>$label</strong> (<em>string</em>) – Field label</li> +<li><strong>$rules</strong> (<em>mixed</em>) – Validation rules, as a string list separated by a pipe “|”, or as an array or rules</li> +<li><strong>$errors</strong> (<em>array</em>) – A list of custom error messages</li> +</ul> +</td> +</tr> +<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> +</td> +</tr> +<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CI_Form_validation</p> +</td> +</tr> +</tbody> +</table> +<p>Permits you to set validation rules, as described in the tutorial +sections above:</p> +<ul class="simple"> +<li><a class="reference internal" href="#setting-validation-rules"><span class="std std-ref">Setting Validation Rules</span></a></li> +<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> +</ul> +</dd></dl> + +<dl class="method"> +<dt id="CI_Form_validation::run"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> +<li><strong>$group</strong> (<em>string</em>) – The name of the validation group to run</li> +</ul> +</td> +</tr> +<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> +</td> +</tr> +<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">bool</p> +</td> +</tr> +</tbody> +</table> +<p>Runs the validation routines. Returns boolean TRUE on success and FALSE +on failure. You can optionally pass the name of the validation group via +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> +</dd></dl> + +<dl class="method"> +<dt id="CI_Form_validation::set_message"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> +<li><strong>$lang</strong> (<em>string</em>) – The rule the message is for</li> +<li><strong>$val</strong> (<em>string</em>) – The message</li> +</ul> +</td> +</tr> +<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> +</td> +</tr> +<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CI_Form_validation</p> +</td> +</tr> +</tbody> +</table> +<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> +</dd></dl> + +<dl class="method"> +<dt id="CI_Form_validation::set_error_delimiters"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> +<li><strong>$prefix</strong> (<em>string</em>) – Error message prefix</li> +<li><strong>$suffix</strong> (<em>string</em>) – Error message suffix</li> +</ul> +</td> +</tr> +<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> +</td> +</tr> +<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CI_Form_validation</p> +</td> +</tr> +</tbody> +</table> +<p>Sets the default prefix and suffix for error messages.</p> +</dd></dl> + +<dl class="method"> +<dt id="CI_Form_validation::set_data"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> +<li><strong>$data</strong> (<em>array</em>) – Array of data validate</li> +</ul> +</td> +</tr> +<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> +</td> +</tr> +<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">CI_Form_validation</p> +</td> +</tr> +</tbody> +</table> +<p>Permits you to set an array for validation, instead of using the default +<code class="docutils literal"><span class="pre">$_POST</span></code> array.</p> +</dd></dl> + +<dl class="method"> +<dt id="CI_Form_validation::reset_validation"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">CI_Form_validation instance (method chaining)</td> +</tr> +<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">CI_Form_validation</td> +</tr> +</tbody> +</table> +<p>Permits you to reset the validation when you validate more than one array. +This method should be called before validating each new array.</p> +</dd></dl> + +<dl class="method"> +<dt id="CI_Form_validation::error_array"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">Array of error messages</td> +</tr> +<tr class="field-even field"><th class="field-name">Return type:</th><td class="field-body">array</td> +</tr> +</tbody> +</table> +<p>Returns the error messages as an array.</p> +</dd></dl> + +<dl class="method"> +<dt id="CI_Form_validation::error_string"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> +<li><strong>$prefix</strong> (<em>string</em>) – Error message prefix</li> +<li><strong>$suffix</strong> (<em>string</em>) – Error message suffix</li> +</ul> +</td> +</tr> +<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">Error messages as a string</p> +</td> +</tr> +<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p> +</td> +</tr> +</tbody> +</table> +<p>Returns all error messages (as returned from error_array()) formatted as a +string and separated by a newline character.</p> +</dd></dl> + +<dl class="method"> +<dt id="CI_Form_validation::error"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> +<li><strong>$field</strong> (<em>string</em>) – Field name</li> +<li><strong>$prefix</strong> (<em>string</em>) – Optional prefix</li> +<li><strong>$suffix</strong> (<em>string</em>) – Optional suffix</li> +</ul> +</td> +</tr> +<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first">Error message string</p> +</td> +</tr> +<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">string</p> +</td> +</tr> +</tbody> +</table> +<p>Returns the error message for a specific field, optionally adding a +prefix and/or suffix to it (usually HTML tags).</p> +</dd></dl> + +<dl class="method"> +<dt id="CI_Form_validation::has_rule"> +<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> +<dd><table class="docutils field-list" frame="void" rules="none"> +<col class="field-name" /> +<col class="field-body" /> +<tbody valign="top"> +<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple"> +<li><strong>$field</strong> (<em>string</em>) – Field name</li> +</ul> +</td> +</tr> +<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> +</td> +</tr> +<tr class="field-odd field"><th class="field-name">Return type:</th><td class="field-body"><p class="first last">bool</p> +</td> +</tr> +</tbody> +</table> +<p>Checks to see if there is a rule set for the specified field.</p> +</dd></dl> + +</dd></dl> + +</div> +<div class="section" id="helper-reference"> +<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> +<p>Please refer to the <a class="reference internal" href="../helpers/form_helper.html"><span class="doc">Form Helper</span></a> manual for +the following functions:</p> +<ul class="simple"> +<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> +<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> +<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> +<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> +<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> +<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> +</ul> +<p>Note that these are procedural functions, so they <strong>do not</strong> require you +to prepend them with <code class="docutils literal"><span class="pre">$this->form_validation</span></code>.</p> +</div> +</div> + + + </div> + <footer> + + <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation"> + + <a href="ftp.html" class="btn btn-neutral float-right" title="FTP Class">Next <span class="fa fa-arrow-circle-right"></span></a> + + + <a href="file_uploading.html" class="btn btn-neutral" title="File Uploading Class"><span class="fa fa-arrow-circle-left"></span> Previous</a> + + </div> + + + <hr/> + + <div role="contentinfo"> + <p> + © Copyright 2014 - 2018, British Columbia Institute of Technology. + Last updated on Jan 13, 2018. + </p> + </div> + + 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>. + +</footer> + </div> + </div> + + </section> + + </div> + + + + + + <script type="text/javascript"> + var DOCUMENTATION_OPTIONS = { + URL_ROOT:'../', + VERSION:'3.1.7', + COLLAPSE_INDEX:false, + FILE_SUFFIX:'.html', + HAS_SOURCE: false + }; + </script> + <script type="text/javascript" src="../_static/jquery.js"></script> + <script type="text/javascript" src="../_static/underscore.js"></script> + <script type="text/javascript" src="../_static/doctools.js"></script> + + + + + + <script type="text/javascript" src="../_static/js/theme.js"></script> + + + + + <script type="text/javascript"> + jQuery(function () { + SphinxRtdTheme.StickyNav.enable(); + }); + </script> + + +</body> +</html>
\ No newline at end of file |