form_validation.html 151 KB

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