ÿŰÿàJFIFHHÿá .
BSA HACKER
Logo of a company Server : Apache
System : Linux nusantara.hosteko.com 4.18.0-553.16.1.lve.el8.x86_64 #1 SMP Tue Aug 13 17:45:03 UTC 2024 x86_64
User : koperas1 ( 1254)
PHP Version : 7.4.33
Disable Function : NONE
Directory :  /home/koperas1/public_html/userguide/libraries/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/koperas1/public_html/userguide/libraries/validation.html

<!DOCTYPE html>
<html class="writer-html4" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>Validation &mdash; CodeIgniter 4.1.1 documentation</title>
  

  
  <link rel="stylesheet" href="../_static/css/citheme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
        <script type="text/javascript" src="../_static/language_data.js"></script>
        <script type="text/javascript" src="../_static/js/citheme.js"></script>
        <script type="text/javascript" src="../_static/js/carbon.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Helpers" href="../helpers/index.html" />
    <link rel="prev" title="User Agent Class" href="user_agent.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search"  style="background: #DD4814" >
          

          
            <a href="../index.html">
          

          
            
            <img src="../_static/ci-logo-text.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../intro/index.html">Welcome to CodeIgniter4</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/requirements.html">Server Requirements</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/credits.html">Credits</a></li>
<li class="toctree-l2"><a class="reference internal" href="../intro/psr.html">PSR Compliance</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../installation/index.html">Installation</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_composer.html">Composer Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/installing_manual.html">Manual Installation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/running.html">Running Your App</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/upgrading.html">Upgrading From a Previous Version</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/troubleshooting.html">Troubleshooting</a></li>
<li class="toctree-l2"><a class="reference internal" href="../installation/repositories.html">CodeIgniter Repositories</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Build Your First Application</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/static_pages.html">Static pages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/news_section.html">News section</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/create_news_items.html">Create news items</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tutorial/conclusion.html">Conclusion</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../concepts/index.html">CodeIgniter4 Overview</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../concepts/structure.html">Application Structure</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/mvc.html">Models, Views, and Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/autoloader.html">Autoloading Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/services.html">Services</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/factories.html">Factories</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/http.html">Working With HTTP Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../concepts/security.html">Security Guidelines</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../general/index.html">General Topics</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../general/configuration.html">Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/urls.html">CodeIgniter URLs</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/helpers.html">Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/common_functions.html">Global Functions and Constants</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/logging.html">Logging Information</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/errors.html">Error Handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/caching.html">Web Page Caching</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/ajax.html">AJAX Requests</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/modules.html">Code Modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/managing_apps.html">Managing your Applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="../general/environments.html">Handling Multiple Environments</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../incoming/index.html">Controllers and Routing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../incoming/controllers.html">Controllers</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/routing.html">URI Routing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/filters.html">Controller Filters</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/message.html">HTTP Messages</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/request.html">Request Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/incomingrequest.html">IncomingRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/content_negotiation.html">Content Negotiation</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/methodspoofing.html">HTTP Method Spoofing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../incoming/restful.html">RESTful Resource Handling</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../outgoing/index.html">Building Responses</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/views.html">Views</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_cells.html">View Cells</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_renderer.html">View Renderer</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_layouts.html">View Layouts</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/view_parser.html">View Parser</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/table.html">HTML Table Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/response.html">HTTP Responses</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/api_responses.html">API Response Trait</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/localization.html">Localization</a></li>
<li class="toctree-l2"><a class="reference internal" href="../outgoing/alternative_php.html">Alternate PHP Syntax for View Files</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../database/index.html">Working With Databases</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../database/examples.html">Quick Start: Usage Examples</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/configuration.html">Database Configuration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/connecting.html">Connecting to a Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/queries.html">Running Queries</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/results.html">Generating Query Results</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/helpers.html">Query Helper Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/query_builder.html">Query Builder Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/transactions.html">Transactions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/metadata.html">Getting MetaData</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/call_function.html">Custom Function Calls</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/events.html">Database Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../database/utilities.html">Database Utilities</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../models/index.html">Modeling Data</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../models/model.html">Using CodeIgniter's Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="../models/entities.html">Using Entity Classes</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../dbmgmt/index.html">Managing Databases</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/forge.html">Database Manipulation with Database Forge</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/migration.html">Database Migrations</a></li>
<li class="toctree-l2"><a class="reference internal" href="../dbmgmt/seeds.html">Database Seeding</a></li>
</ul>
</li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">Library Reference</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="caching.html">Caching Driver</a></li>
<li class="toctree-l2"><a class="reference internal" href="curlrequest.html">CURLRequest Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="email.html">Email Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="encryption.html">Encryption Service</a></li>
<li class="toctree-l2"><a class="reference internal" href="files.html">Working with Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="honeypot.html">Honeypot Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="images.html">Image Manipulation Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="pagination.html">Pagination</a></li>
<li class="toctree-l2"><a class="reference internal" href="security.html">Security</a></li>
<li class="toctree-l2"><a class="reference internal" href="sessions.html">Session Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="throttler.html">Throttler</a></li>
<li class="toctree-l2"><a class="reference internal" href="time.html">Times and Dates</a></li>
<li class="toctree-l2"><a class="reference internal" href="typography.html">Typography</a></li>
<li class="toctree-l2"><a class="reference internal" href="uploaded_files.html">Working with Uploaded Files</a></li>
<li class="toctree-l2"><a class="reference internal" href="uri.html">Working with URIs</a></li>
<li class="toctree-l2"><a class="reference internal" href="user_agent.html">User Agent Class</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Validation</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../helpers/index.html">Helpers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../helpers/array_helper.html">Array Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/cookie_helper.html">Cookie Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/date_helper.html">Date Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/filesystem_helper.html">Filesystem Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/form_helper.html">Form Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/html_helper.html">HTML Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/inflector_helper.html">Inflector Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/number_helper.html">Number Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/security_helper.html">Security Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/test_helper.html">Test Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/text_helper.html">Text Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/url_helper.html">URL Helper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../helpers/xml_helper.html">XML Helper</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../testing/index.html">Testing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../testing/overview.html">Getting Started</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/database.html">Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/fabricator.html">Generating Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/controllers.html">Controller Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/feature.html">HTTP Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/benchmark.html">Benchmarking</a></li>
<li class="toctree-l2"><a class="reference internal" href="../testing/debugging.html">Debugging Your Application</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../cli/index.html">Command Line Usage</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli.html">Running via the Command Line</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_commands.html">Custom CLI Commands</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_generators.html">CLI Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_library.html">CLI Library</a></li>
<li class="toctree-l2"><a class="reference internal" href="../cli/cli_request.html">CLIRequest Class</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../extending/index.html">Extending CodeIgniter</a><ul>
<li class="toctree-l2"><a class="reference internal" href="../extending/core_classes.html">Creating Core System Classes</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/common.html">Replacing Common Functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/events.html">Events</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/basecontroller.html">Extending the Controller</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/authentication.html">Authentication</a></li>
<li class="toctree-l2"><a class="reference internal" href="../extending/contributing.html">Contributing to CodeIgniter</a></li>
</ul>
</li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">CodeIgniter</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="index.html">Library Reference</a> &raquo;</li>
        
      <li>Validation</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="validation">
<h1>Validation<a class="headerlink" href="#validation" title="Permalink to this headline">¶</a></h1>
<p>CodeIgniter provides a comprehensive data validation class that
helps minimize the amount of code you’ll write.</p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#overview" id="id1">Overview</a></li>
<li><a class="reference internal" href="#form-validation-tutorial" id="id2">Form Validation Tutorial</a><ul>
<li><a class="reference internal" href="#the-form" id="id3">The Form</a></li>
<li><a class="reference internal" href="#the-success-page" id="id4">The Success Page</a></li>
<li><a class="reference internal" href="#the-controller" id="id5">The Controller</a></li>
<li><a class="reference internal" href="#try-it" id="id6">Try it!</a></li>
<li><a class="reference internal" href="#explanation" id="id7">Explanation</a></li>
<li><a class="reference internal" href="#loading-the-library" id="id8">Loading the Library</a></li>
<li><a class="reference internal" href="#setting-validation-rules" id="id9">Setting Validation Rules</a></li>
</ul>
</li>
<li><a class="reference internal" href="#working-with-validation" id="id10">Working with Validation</a><ul>
<li><a class="reference internal" href="#validating-keys-that-are-arrays" id="id11">Validating Keys that are Arrays</a></li>
<li><a class="reference internal" href="#validate-1-value" id="id12">Validate 1 Value</a></li>
<li><a class="reference internal" href="#saving-sets-of-validation-rules-to-the-config-file" id="id13">Saving Sets of Validation Rules to the Config File</a></li>
<li><a class="reference internal" href="#running-multiple-validations" id="id14">Running Multiple Validations</a></li>
<li><a class="reference internal" href="#validation-placeholders" id="id15">Validation Placeholders</a></li>
</ul>
</li>
<li><a class="reference internal" href="#working-with-errors" id="id16">Working With Errors</a><ul>
<li><a class="reference internal" href="#setting-custom-error-messages" id="id17">Setting Custom Error Messages</a></li>
<li><a class="reference internal" href="#translation-of-messages-and-validation-labels" id="id18">Translation Of Messages And Validation Labels</a></li>
<li><a class="reference internal" href="#getting-all-errors" id="id19">Getting All Errors</a></li>
<li><a class="reference internal" href="#getting-a-single-error" id="id20">Getting a Single Error</a></li>
<li><a class="reference internal" href="#check-if-error-exists" id="id21">Check If Error Exists</a></li>
</ul>
</li>
<li><a class="reference internal" href="#customizing-error-display" id="id22">Customizing Error Display</a><ul>
<li><a class="reference internal" href="#creating-the-views" id="id23">Creating the Views</a></li>
<li><a class="reference internal" href="#configuration" id="id24">Configuration</a></li>
<li><a class="reference internal" href="#specifying-the-template" id="id25">Specifying the Template</a></li>
</ul>
</li>
<li><a class="reference internal" href="#creating-custom-rules" id="id26">Creating Custom Rules</a><ul>
<li><a class="reference internal" href="#allowing-parameters" id="id27">Allowing Parameters</a></li>
</ul>
</li>
<li><a class="reference internal" href="#available-rules" id="id28">Available Rules</a><ul>
<li><a class="reference internal" href="#rules-for-file-uploads" id="id29">Rules for File Uploads</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="overview">
<h2><a class="toc-backref" href="#id1">Overview</a><a class="headerlink" href="#overview" title="Permalink to this headline">¶</a></h2>
<p>Before explaining CodeIgniter’s approach to data validation, let’s
describe the ideal scenario:</p>
<ol class="arabic simple">
<li>A form is displayed.</li>
<li>You fill it in and submit it.</li>
<li>If you submitted something invalid, or perhaps missed a required
item, the form is redisplayed containing your data along with an
error message describing the problem.</li>
<li>This process continues until you have submitted a valid form.</li>
</ol>
<p>On the receiving end, the script must:</p>
<ol class="arabic simple">
<li>Check for required data.</li>
<li>Verify that the data is of the correct type, and meets the correct
criteria. For example, if a username is submitted it must be
validated to contain only permitted characters. It must be of a
minimum length, and not exceed a maximum length. The username can’t
be someone else’s existing username, or perhaps even a reserved word.
Etc.</li>
<li>Sanitize the data for security.</li>
<li>Pre-format the data if needed (Does the data need to be trimmed? HTML
encoded? Etc.)</li>
<li>Prep the data for insertion in the database.</li>
</ol>
<p>Although there is nothing terribly complex about the above process, it
usually requires a significant amount of code, and to display error
messages, various control structures are usually placed within the form
HTML. Form validation, while simple to create, is generally very messy
and tedious to implement.</p>
</div>
<div class="section" id="form-validation-tutorial">
<h2><a class="toc-backref" href="#id2">Form Validation Tutorial</a><a class="headerlink" href="#form-validation-tutorial" title="Permalink to this headline">¶</a></h2>
<p>What follows is a “hands on” tutorial for implementing CodeIgniter’s Form
Validation.</p>
<p>In order to implement form validation you’ll need three things:</p>
<ol class="arabic simple">
<li>A <a class="reference internal" href="../outgoing/views.html"><span class="doc">View</span></a> file containing a form.</li>
<li>A View file containing a “success” message to be displayed upon
successful submission.</li>
<li>A <a class="reference internal" href="../incoming/controllers.html"><span class="doc">controller</span></a> method to receive and
process the submitted data.</li>
</ol>
<p>Let’s create those three things, using a member sign-up form as the
example.</p>
<div class="section" id="the-form">
<h3><a class="toc-backref" href="#id3">The Form</a><a class="headerlink" href="#the-form" title="Permalink to this headline">¶</a></h3>
<p>Using a text editor, create a form called <strong>Signup.php</strong>. In it, place this
code and save it to your <strong>app/Views/</strong> folder:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="nx">html</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="nx">head</span><span class="o">&gt;</span>
    <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>
<span class="o">&lt;/</span><span class="nx">head</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="nx">body</span><span class="o">&gt;</span>

    <span class="o">&lt;?=</span> <span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">listErrors</span><span class="p">()</span> <span class="cp">?&gt;</span>

    <span class="cp">&lt;?</span><span class="o">=</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>

    <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>
    <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>

    <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>
    <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>

    <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>
    <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>

    <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>
    <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>

    <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>

    <span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>

<span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="the-success-page">
<h3><a class="toc-backref" href="#id4">The Success Page</a><a class="headerlink" href="#the-success-page" title="Permalink to this headline">¶</a></h3>
<p>Using a text editor, create a form called <strong>Success.php</strong>. In it, place
this code and save it to your <strong>app/Views/</strong> folder:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="nx">html</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="nx">head</span><span class="o">&gt;</span>
    <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>
<span class="o">&lt;/</span><span class="nx">head</span><span class="o">&gt;</span>
<span class="o">&lt;</span><span class="nx">body</span><span class="o">&gt;</span>

    <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>

    <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;&lt;?=</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>

<span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="the-controller">
<h3><a class="toc-backref" href="#id5">The Controller</a><a class="headerlink" href="#the-controller" title="Permalink to this headline">¶</a></h3>
<p>Using a text editor, create a controller called <strong>Form.php</strong>. In it, place
this code and save it to your <strong>app/Controllers/</strong> folder:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?</span><span class="nx">php</span>

<span class="k">namespace</span> <span class="nx">App\Controllers</span><span class="p">;</span>

<span class="k">use</span> <span class="nx">CodeIgniter\Controller</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Form</span> <span class="k">extends</span> <span class="nx">Controller</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">index</span><span class="p">()</span>
    <span class="p">{</span>
        <span class="nx">helper</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>

        <span class="k">if</span> <span class="p">(</span><span class="o">!</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">validate</span><span class="p">([]))</span>
        <span class="p">{</span>
            <span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">&#39;Signup&#39;</span><span class="p">,</span> <span class="p">[</span>
                <span class="s1">&#39;validation&#39;</span> <span class="o">=&gt;</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">validator</span><span class="p">,</span>
            <span class="p">]);</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="k">echo</span> <span class="nx">view</span><span class="p">(</span><span class="s1">&#39;Success&#39;</span><span class="p">);</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="try-it">
<h3><a class="toc-backref" href="#id6">Try it!</a><a class="headerlink" href="#try-it" title="Permalink to this headline">¶</a></h3>
<p>To try your form, visit your site using a URL similar to this one:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nx">example</span><span class="o">.</span><span class="nx">com</span><span class="o">/</span><span class="nx">index</span><span class="o">.</span><span class="nx">php</span><span class="o">/</span><span class="nx">form</span><span class="o">/</span>
</pre></div>
</div>
<p>If you submit the form you should simply see the form reload. That’s
because you haven’t set up any validation rules yet.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Since you haven’t told the <strong>Validation class</strong> to validate anything
yet, it <strong>returns false</strong> (boolean false) <strong>by default</strong>. The <code class="docutils literal notranslate"><span class="pre">validate()</span></code>
method only returns true if it has successfully applied your rules without
any of them failing.</p>
</div>
</div>
<div class="section" id="explanation">
<h3><a class="toc-backref" href="#id7">Explanation</a><a class="headerlink" href="#explanation" title="Permalink to this headline">¶</a></h3>
<p>You’ll notice several things about the above pages:</p>
<p>The form (Signup.php) is a standard web form with a couple of exceptions:</p>
<ol class="arabic">
<li><p class="first">It uses a form helper to create the form opening. Technically, this
isn’t necessary. You could create the form using standard HTML.
However, the benefit of using the helper is that it generates the
action URL for you, based on the URL in your config file. This makes
your application more portable in the event your URLs change.</p>
</li>
<li><p class="first">At the top of the form you’ll notice the following function call:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?=</span> <span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">listErrors</span><span class="p">()</span> <span class="cp">?&gt;</span>
</pre></div>
</div>
<p>This function will return any error messages sent back by the
validator. If there are no messages it returns an empty string.</p>
</li>
</ol>
<p>The controller (Form.php) has one method: <code class="docutils literal notranslate"><span class="pre">index()</span></code>. This method
uses the Controller-provided validate method and loads the form helper and URL
helper used by your view files. It also runs the validation routine.
Based on whether the validation was successful it either presents the
form or the success page.</p>
</div>
<div class="section" id="loading-the-library">
<h3><a class="toc-backref" href="#id8">Loading the Library</a><a class="headerlink" href="#loading-the-library" title="Permalink to this headline">¶</a></h3>
<p>The library is loaded as a service named <strong>validation</strong>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span> <span class="o">=</span>  <span class="nx">\Config\Services</span><span class="o">::</span><span class="na">validation</span><span class="p">();</span>
</pre></div>
</div>
<p>This automatically loads the <code class="docutils literal notranslate"><span class="pre">Config\Validation</span></code> file which contains settings
for including multiple Rulesets, and collections of rules that can be easily reused.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You may never need to use this method, as both the <a class="reference internal" href="../incoming/controllers.html"><span class="doc">Controller</span></a> and
the <a class="reference internal" href="../models/model.html"><span class="doc">Model</span></a> provide methods to make validation even easier.</p>
</div>
</div>
<div class="section" id="setting-validation-rules">
<h3><a class="toc-backref" href="#id9">Setting Validation Rules</a><a class="headerlink" href="#setting-validation-rules" title="Permalink to this headline">¶</a></h3>
<p>CodeIgniter lets you set as many validation rules as you need for a
given field, cascading them in order. To set validation rules you
will use the <code class="docutils literal notranslate"><span class="pre">setRule()</span></code>, <code class="docutils literal notranslate"><span class="pre">setRules()</span></code>, or <code class="docutils literal notranslate"><span class="pre">withRequest()</span></code>
methods.</p>
<div class="section" id="setrule">
<h4>setRule()<a class="headerlink" href="#setrule" title="Permalink to this headline">¶</a></h4>
<p>This method sets a single rule. It takes the name of the field as
the first parameter, an optional label and a string with a pipe-delimited list of rules
that should be applied:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRule</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>
</pre></div>
</div>
<p>The <strong>field name</strong> must match the key of any data array that is sent in. If
the data is taken directly from $_POST, then it must be an exact match for
the form input name.</p>
</div>
<div class="section" id="setrules">
<h4>setRules()<a class="headerlink" href="#setrules" title="Permalink to this headline">¶</a></h4>
<p>Like, <code class="docutils literal notranslate"><span class="pre">setRule()</span></code>, but accepts an array of field names and their rules:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
    <span class="s1">&#39;username&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span><span class="p">,</span>
    <span class="s1">&#39;password&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required|min_length[10]&#39;</span>
<span class="p">]);</span>
</pre></div>
</div>
<p>To give a labeled error message you can set up as:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
    <span class="s1">&#39;username&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;label&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Username&#39;</span><span class="p">,</span> <span class="s1">&#39;rules&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span><span class="p">],</span>
    <span class="s1">&#39;password&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span><span class="s1">&#39;label&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Password&#39;</span><span class="p">,</span> <span class="s1">&#39;rules&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required|min_length[10]&#39;</span><span class="p">]</span>
<span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="withrequest">
<h4>withRequest()<a class="headerlink" href="#withrequest" title="Permalink to this headline">¶</a></h4>
<p>One of the most common times you will use the validation library is when validating
data that was input from an HTTP Request. If desired, you can pass an instance of the
current Request object and it will take all of the input data and set it as the
data to be validated:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">withRequest</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">request</span><span class="p">)</span>
           <span class="o">-&gt;</span><span class="na">run</span><span class="p">();</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="working-with-validation">
<h2><a class="toc-backref" href="#id10">Working with Validation</a><a class="headerlink" href="#working-with-validation" title="Permalink to this headline">¶</a></h2>
<div class="section" id="validating-keys-that-are-arrays">
<h3><a class="toc-backref" href="#id11">Validating Keys that are Arrays</a><a class="headerlink" href="#validating-keys-that-are-arrays" title="Permalink to this headline">¶</a></h3>
<p>If your data is in a nested associative array, you can use “dot array syntax” to
easily validate your data:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// The data to test:</span>
<span class="s1">&#39;contacts&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
    <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Joe Smith&#39;</span><span class="p">,</span>
    <span class="s1">&#39;friends&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
        <span class="p">[</span>
            <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Fred Flinstone&#39;</span>
        <span class="p">],</span>
        <span class="p">[</span>
            <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Wilma&#39;</span>
        <span class="p">]</span>
    <span class="p">]</span>
<span class="p">]</span>

<span class="c1">// Joe Smith</span>
<span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
    <span class="s1">&#39;contacts.name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span>
<span class="p">]);</span>

<span class="c1">// Fred Flintsone &amp; Wilma</span>
<span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
    <span class="s1">&#39;contacts.friends.name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span>
<span class="p">]);</span>
</pre></div>
</div>
<p>You can use the ‘*’ wildcard symbol to match any one level of the array:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// Fred Flintsone &amp; Wilma</span>
<span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
    <span class="s1">&#39;contacts.*.name&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span>
<span class="p">]);</span>
</pre></div>
</div>
<p>“dot array syntax” can also be useful when you have single dimension array data.
For example, data returned by multi select dropdown:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// The data to test:</span>
<span class="s1">&#39;user_ids&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
    <span class="mi">1</span><span class="p">,</span>
    <span class="mi">2</span><span class="p">,</span>
    <span class="mi">3</span>
<span class="p">]</span>
<span class="c1">// Rule</span>
<span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
    <span class="s1">&#39;user_ids.*&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span>
<span class="p">]);</span>
</pre></div>
</div>
</div>
<div class="section" id="validate-1-value">
<h3><a class="toc-backref" href="#id12">Validate 1 Value</a><a class="headerlink" href="#validate-1-value" title="Permalink to this headline">¶</a></h3>
<p>Validate one value against a rule:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">check</span><span class="p">(</span><span class="nv">$value</span><span class="p">,</span> <span class="s1">&#39;required&#39;</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="saving-sets-of-validation-rules-to-the-config-file">
<h3><a class="toc-backref" href="#id13">Saving Sets of Validation Rules to the Config File</a><a class="headerlink" href="#saving-sets-of-validation-rules-to-the-config-file" title="Permalink to this headline">¶</a></h3>
<p>A nice feature of the Validation class is that it permits you to store all
your validation rules for your entire application in a config file. You organize
the rules into “groups”. You can specify a different group every time you run
the validation.</p>
<div class="section" id="how-to-save-your-rules">
<span id="validation-array"></span><h4>How to save your rules<a class="headerlink" href="#how-to-save-your-rules" title="Permalink to this headline">¶</a></h4>
<p>To store your validation rules, simply create a new public property in the <code class="docutils literal notranslate"><span class="pre">Config\Validation</span></code>
class with the name of your group. This element will hold an array with your validation
rules. As shown earlier, the validation array will have this prototype:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Validation</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="nv">$signup</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;username&#39;</span>     <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span><span class="p">,</span>
        <span class="s1">&#39;password&#39;</span>     <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span><span class="p">,</span>
        <span class="s1">&#39;pass_confirm&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required|matches[password]&#39;</span><span class="p">,</span>
        <span class="s1">&#39;email&#39;</span>        <span class="o">=&gt;</span> <span class="s1">&#39;required|valid_email&#39;</span>
    <span class="p">];</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You can specify the group to use when you call the <code class="docutils literal notranslate"><span class="pre">run()</span></code> method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">run</span><span class="p">(</span><span class="nv">$data</span><span class="p">,</span> <span class="s1">&#39;signup&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>You can also store custom error messages in this configuration file by naming the
property the same as the group, and appended with <code class="docutils literal notranslate"><span class="pre">_errors</span></code>. These will automatically
be used for any errors when this group is used:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Validation</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="nv">$signup</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;username&#39;</span>     <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span><span class="p">,</span>
        <span class="s1">&#39;password&#39;</span>     <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span><span class="p">,</span>
        <span class="s1">&#39;pass_confirm&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required|matches[password]&#39;</span><span class="p">,</span>
        <span class="s1">&#39;email&#39;</span>        <span class="o">=&gt;</span> <span class="s1">&#39;required|valid_email&#39;</span>
    <span class="p">];</span>

    <span class="k">public</span> <span class="nv">$signup_errors</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;username&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;required&#39;</span>    <span class="o">=&gt;</span> <span class="s1">&#39;You must choose a username.&#39;</span><span class="p">,</span>
        <span class="p">],</span>
        <span class="s1">&#39;email&#39;</span>    <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;valid_email&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Please check the Email field. It does not appear to be valid.&#39;</span>
        <span class="p">]</span>
    <span class="p">];</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Or pass all settings in an array:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Validation</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="nv">$signup</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s1">&#39;username&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;rules&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;required&#39;</span><span class="p">,</span>
            <span class="s1">&#39;errors&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                <span class="s1">&#39;required&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;You must choose a Username.&#39;</span>
            <span class="p">]</span>
        <span class="p">],</span>
        <span class="s1">&#39;email&#39;</span>    <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;rules&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;required|valid_email&#39;</span><span class="p">,</span>
            <span class="s1">&#39;errors&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                <span class="s1">&#39;valid_email&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Please check the Email field. It does not appear to be valid.&#39;</span>
            <span class="p">]</span>
        <span class="p">],</span>
    <span class="p">];</span>
<span class="p">}</span>
</pre></div>
</div>
<p>See below for details on the formatting of the array.</p>
</div>
<div class="section" id="getting-setting-rule-groups">
<h4>Getting &amp; Setting Rule Groups<a class="headerlink" href="#getting-setting-rule-groups" title="Permalink to this headline">¶</a></h4>
<p><strong>Get Rule Group</strong></p>
<p>This method gets a rule group from the validation configuration:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">getRuleGroup</span><span class="p">(</span><span class="s1">&#39;signup&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p><strong>Set Rule Group</strong></p>
<p>This method sets a rule group from the validation configuration to the validation service:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRuleGroup</span><span class="p">(</span><span class="s1">&#39;signup&#39;</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="running-multiple-validations">
<h3><a class="toc-backref" href="#id14">Running Multiple Validations</a><a class="headerlink" href="#running-multiple-validations" title="Permalink to this headline">¶</a></h3>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">run()</span></code> method will not reset error state. Should a previous run fail,
<code class="docutils literal notranslate"><span class="pre">run()</span></code> will always return false and <code class="docutils literal notranslate"><span class="pre">getErrors()</span></code> will return
all previous errors until explicitly reset.</p>
</div>
<p>If you intend to run multiple validations, for instance on different data sets or with different
rules after one another, you might need to call <code class="docutils literal notranslate"><span class="pre">$validation-&gt;reset()</span></code> before each run to get rid of
errors from previous run. Be aware that <code class="docutils literal notranslate"><span class="pre">reset()</span></code> will invalidate any data, rule or custom error
you previously set, so <code class="docutils literal notranslate"><span class="pre">setRules()</span></code>, <code class="docutils literal notranslate"><span class="pre">setRuleGroup()</span></code> etc. need to be repeated:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="p">(</span><span class="nv">$userAccounts</span> <span class="k">as</span> <span class="nv">$user</span><span class="p">)</span> <span class="p">{</span>
    <span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">reset</span><span class="p">();</span>
    <span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">(</span><span class="nv">$userAccountRules</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">run</span><span class="p">(</span><span class="nv">$user</span><span class="p">))</span> <span class="p">{</span>
        <span class="c1">// handle validation errors</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="validation-placeholders">
<h3><a class="toc-backref" href="#id15">Validation Placeholders</a><a class="headerlink" href="#validation-placeholders" title="Permalink to this headline">¶</a></h3>
<p>The Validation class provides a simple method to replace parts of your rules based on data that’s being passed into it. This
sounds fairly obscure but can be especially handy with the <code class="docutils literal notranslate"><span class="pre">is_unique</span></code> validation rule. Placeholders are simply
the name of the field (or array key) that was passed in as $data surrounded by curly brackets. It will be
replaced by the <strong>value</strong> of the matched incoming field. An example should clarify this:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
    <span class="s1">&#39;email&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required|valid_email|is_unique[users.email,id,{id}]&#39;</span>
<span class="p">]);</span>
</pre></div>
</div>
<p>In this set of rules, it states that the email address should be unique in the database, except for the row
that has an id matching the placeholder’s value. Assuming that the form POST data had the following:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$_POST</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;id&#39;</span> <span class="o">=&gt;</span> <span class="mi">4</span><span class="p">,</span>
    <span class="s1">&#39;email&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;foo@example.com&#39;</span>
<span class="p">];</span>
</pre></div>
</div>
<p>then the <code class="docutils literal notranslate"><span class="pre">{id}</span></code> placeholder would be replaced with the number <strong>4</strong>, giving this revised rule:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
    <span class="s1">&#39;email&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required|valid_email|is_unique[users.email,id,4]&#39;</span>
<span class="p">]);</span>
</pre></div>
</div>
<p>So it will ignore the row in the database that has <code class="docutils literal notranslate"><span class="pre">id=4</span></code> when it verifies the email is unique.</p>
<p>This can also be used to create more dynamic rules at runtime, as long as you take care that any dynamic
keys passed in don’t conflict with your form data.</p>
</div>
</div>
<div class="section" id="working-with-errors">
<h2><a class="toc-backref" href="#id16">Working With Errors</a><a class="headerlink" href="#working-with-errors" title="Permalink to this headline">¶</a></h2>
<p>The Validation library provides several methods to help you set error messages, provide
custom error messages, and retrieve one or more errors to display.</p>
<p>By default, error messages are derived from language strings in <code class="docutils literal notranslate"><span class="pre">system/Language/en/Validation.php</span></code>, where
each rule has an entry.</p>
<div class="section" id="setting-custom-error-messages">
<span id="validation-custom-errors"></span><h3><a class="toc-backref" href="#id17">Setting Custom Error Messages</a><a class="headerlink" href="#setting-custom-error-messages" title="Permalink to this headline">¶</a></h3>
<p>Both the <code class="docutils literal notranslate"><span class="pre">setRule()</span></code> and <code class="docutils literal notranslate"><span class="pre">setRules()</span></code> methods can accept an array of custom messages
that will be used as errors specific to each field as their last parameter. This allows
for a very pleasant experience for the user since the errors are tailored to each
instance. If not custom error message is provided, the default value will be used.</p>
<p>These are two ways to provide custom error messages.</p>
<p>As the last parameter:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
        <span class="s1">&#39;username&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required|is_unique[users.username]&#39;</span><span class="p">,</span>
        <span class="s1">&#39;password&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required|min_length[10]&#39;</span>
    <span class="p">],</span>
    <span class="p">[</span>   <span class="c1">// Errors</span>
        <span class="s1">&#39;username&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;required&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;All accounts must have usernames provided&#39;</span><span class="p">,</span>
        <span class="p">],</span>
        <span class="s1">&#39;password&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;min_length&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Your password is too short. You want to get hacked?&#39;</span>
        <span class="p">]</span>
    <span class="p">]</span>
<span class="p">);</span>
</pre></div>
</div>
<p>Or as a labeled style:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
        <span class="s1">&#39;username&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;label&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;Username&#39;</span><span class="p">,</span>
            <span class="s1">&#39;rules&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;required|is_unique[users.username]&#39;</span><span class="p">,</span>
            <span class="s1">&#39;errors&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                <span class="s1">&#39;required&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;All accounts must have {field} provided&#39;</span>
            <span class="p">]</span>
        <span class="p">],</span>
        <span class="s1">&#39;password&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;label&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;Password&#39;</span><span class="p">,</span>
            <span class="s1">&#39;rules&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;required|min_length[10]&#39;</span><span class="p">,</span>
            <span class="s1">&#39;errors&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                <span class="s1">&#39;min_length&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Your {field} is too short. You want to get hacked?&#39;</span>
            <span class="p">]</span>
        <span class="p">]</span>
    <span class="p">]</span>
<span class="p">);</span>
</pre></div>
</div>
<p>If you’d like to include a field’s “human” name, or the optional parameter some rules allow for (such as max_length),
or the value that was validated you can add the <code class="docutils literal notranslate"><span class="pre">{field}</span></code>, <code class="docutils literal notranslate"><span class="pre">{param}</span></code> and <code class="docutils literal notranslate"><span class="pre">{value}</span></code> tags to your message, respectively:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="s1">&#39;min_length&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Supplied value ({value}) for {field} must have at least {param} characters.&#39;</span>
</pre></div>
</div>
<p>On a field with the human name Username and a rule of min_length[6] with a value of “Pizza”, an error would display: “Supplied value (Pizza) for Username must have
at least 6 characters.”</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you pass the last parameter the labeled style error messages will be ignored.</p>
</div>
</div>
<div class="section" id="translation-of-messages-and-validation-labels">
<h3><a class="toc-backref" href="#id18">Translation Of Messages And Validation Labels</a><a class="headerlink" href="#translation-of-messages-and-validation-labels" title="Permalink to this headline">¶</a></h3>
<p>To use translated strings from language files, we can simply use the dot syntax.
Let’s say we have a file with translations located here: <code class="docutils literal notranslate"><span class="pre">app/Languages/en/Rules.php</span></code>.
We can simply use the language lines defined in this file, like this:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
        <span class="s1">&#39;username&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;label&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;Rules.username&#39;</span><span class="p">,</span>
            <span class="s1">&#39;rules&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;required|is_unique[users.username]&#39;</span><span class="p">,</span>
            <span class="s1">&#39;errors&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                <span class="s1">&#39;required&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Rules.username.required&#39;</span>
            <span class="p">]</span>
        <span class="p">],</span>
        <span class="s1">&#39;password&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
            <span class="s1">&#39;label&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;Rules.password&#39;</span><span class="p">,</span>
            <span class="s1">&#39;rules&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;required|min_length[10]&#39;</span><span class="p">,</span>
            <span class="s1">&#39;errors&#39;</span> <span class="o">=&gt;</span> <span class="p">[</span>
                <span class="s1">&#39;min_length&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;Rules.password.min_length&#39;</span>
            <span class="p">]</span>
        <span class="p">]</span>
    <span class="p">]</span>
<span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="getting-all-errors">
<h3><a class="toc-backref" href="#id19">Getting All Errors</a><a class="headerlink" href="#getting-all-errors" title="Permalink to this headline">¶</a></h3>
<p>If you need to retrieve all error messages for failed fields, you can use the <code class="docutils literal notranslate"><span class="pre">getErrors()</span></code> method:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$errors</span> <span class="o">=</span> <span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">getErrors</span><span class="p">();</span>

<span class="c1">// Returns:</span>
<span class="p">[</span>
    <span class="s1">&#39;field1&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;error message&#39;</span><span class="p">,</span>
    <span class="s1">&#39;field2&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;error message&#39;</span><span class="p">,</span>
<span class="p">]</span>
</pre></div>
</div>
<p>If no errors exist, an empty array will be returned.</p>
</div>
<div class="section" id="getting-a-single-error">
<h3><a class="toc-backref" href="#id20">Getting a Single Error</a><a class="headerlink" href="#getting-a-single-error" title="Permalink to this headline">¶</a></h3>
<p>You can retrieve the error for a single field with the <code class="docutils literal notranslate"><span class="pre">getError()</span></code> method. The only parameter is the field
name:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$error</span> <span class="o">=</span> <span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">getError</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>If no error exists, an empty string will be returned.</p>
</div>
<div class="section" id="check-if-error-exists">
<h3><a class="toc-backref" href="#id21">Check If Error Exists</a><a class="headerlink" href="#check-if-error-exists" title="Permalink to this headline">¶</a></h3>
<p>You can check to see if an error exists with the <code class="docutils literal notranslate"><span class="pre">hasError()</span></code> method. The only parameter is the field name:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="p">(</span><span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">hasError</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">))</span>
<span class="p">{</span>
    <span class="k">echo</span> <span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">getError</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="customizing-error-display">
<h2><a class="toc-backref" href="#id22">Customizing Error Display</a><a class="headerlink" href="#customizing-error-display" title="Permalink to this headline">¶</a></h2>
<p>When you call <code class="docutils literal notranslate"><span class="pre">$validation-&gt;listErrors()</span></code> or <code class="docutils literal notranslate"><span class="pre">$validation-&gt;showError()</span></code>, it loads a view file in the background
that determines how the errors are displayed. By default, they display with a class of <code class="docutils literal notranslate"><span class="pre">errors</span></code> on the wrapping div.
You can easily create new views and use them throughout your application.</p>
<div class="section" id="creating-the-views">
<h3><a class="toc-backref" href="#id23">Creating the Views</a><a class="headerlink" href="#creating-the-views" title="Permalink to this headline">¶</a></h3>
<p>The first step is to create custom views. These can be placed anywhere that the <code class="docutils literal notranslate"><span class="pre">view()</span></code> method can locate them,
which means the standard View directory, or any namespaced View folder will work. For example, you could create
a new view at <strong>/app/Views/_errors_list.php</strong>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="nx">div</span> <span class="nx">class</span><span class="o">=</span><span class="s2">&quot;alert alert-danger&quot;</span> <span class="nx">role</span><span class="o">=</span><span class="s2">&quot;alert&quot;</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="nx">ul</span><span class="o">&gt;</span>
    <span class="o">&lt;?</span><span class="nx">php</span> <span class="k">foreach</span> <span class="p">(</span><span class="nv">$errors</span> <span class="k">as</span> <span class="nv">$error</span><span class="p">)</span> <span class="o">:</span> <span class="cp">?&gt;</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">&lt;?</span><span class="o">=</span> <span class="nx">esc</span><span class="p">(</span><span class="nv">$error</span><span class="p">)</span> <span class="cp">?&gt;</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">&lt;?php</span> <span class="k">endforeach</span> <span class="cp">?&gt;</span>
    <span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>An array named <code class="docutils literal notranslate"><span class="pre">$errors</span></code> is available within the view that contains a list of the errors, where the key is
the name of the field that had the error, and the value is the error message, like this:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$errors</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;username&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;The username field must be unique.&#39;</span><span class="p">,</span>
    <span class="s1">&#39;email&#39;</span>    <span class="o">=&gt;</span> <span class="s1">&#39;You must provide a valid email address.&#39;</span>
<span class="p">];</span>
</pre></div>
</div>
<p>There are actually two types of views that you can create. The first has an array of all of the errors, and is what
we just looked at. The other type is simpler, and only contains a single variable, <code class="docutils literal notranslate"><span class="pre">$error</span></code> that contains the
error message. This is used with the <code class="docutils literal notranslate"><span class="pre">showError()</span></code> method where a field must be specified:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;</span><span class="nx">span</span> <span class="nx">class</span><span class="o">=</span><span class="s2">&quot;help-block&quot;</span><span class="o">&gt;&lt;?=</span> <span class="nx">esc</span><span class="p">(</span><span class="nv">$error</span><span class="p">)</span> <span class="cp">?&gt;</span><span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="configuration">
<h3><a class="toc-backref" href="#id24">Configuration</a><a class="headerlink" href="#configuration" title="Permalink to this headline">¶</a></h3>
<p>Once you have your views created, you need to let the Validation library know about them. Open <code class="docutils literal notranslate"><span class="pre">Config/Validation.php</span></code>.
Inside, you’ll find the <code class="docutils literal notranslate"><span class="pre">$templates</span></code> property where you can list as many custom views as you want, and provide an
short alias they can be referenced by. If we were to add our example file from above, it would look something like:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nv">$templates</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;list&#39;</span>    <span class="o">=&gt;</span> <span class="s1">&#39;CodeIgniter\Validation\Views\list&#39;</span><span class="p">,</span>
    <span class="s1">&#39;single&#39;</span>  <span class="o">=&gt;</span> <span class="s1">&#39;CodeIgniter\Validation\Views\single&#39;</span><span class="p">,</span>
    <span class="s1">&#39;my_list&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;_errors_list&#39;</span>
<span class="p">];</span>
</pre></div>
</div>
</div>
<div class="section" id="specifying-the-template">
<h3><a class="toc-backref" href="#id25">Specifying the Template</a><a class="headerlink" href="#specifying-the-template" title="Permalink to this headline">¶</a></h3>
<p>You can specify the template to use by passing it’s alias as the first parameter in <code class="docutils literal notranslate"><span class="pre">listErrors</span></code>:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?=</span> <span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">listErrors</span><span class="p">(</span><span class="s1">&#39;my_list&#39;</span><span class="p">)</span> <span class="cp">?&gt;</span>
</pre></div>
</div>
<p>When showing field-specific errors, you can pass the alias as the second parameter to the <code class="docutils literal notranslate"><span class="pre">showError</span></code> method,
right after the name of the field the error should belong to:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="o">&lt;?=</span> <span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">showError</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="s1">&#39;my_single&#39;</span><span class="p">)</span> <span class="cp">?&gt;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="creating-custom-rules">
<h2><a class="toc-backref" href="#id26">Creating Custom Rules</a><a class="headerlink" href="#creating-custom-rules" title="Permalink to this headline">¶</a></h2>
<p>Rules are stored within simple, namespaced classes. They can be stored any location you would like, as long as the
autoloader can find it. These files are called RuleSets. To add a new RuleSet, edit <strong>Config/Validation.php</strong> and
add the new file to the <code class="docutils literal notranslate"><span class="pre">$ruleSets</span></code> array:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="nv">$ruleSets</span> <span class="o">=</span> <span class="p">[</span>
    <span class="nx">\CodeIgniter\Validation\Rules</span><span class="o">::</span><span class="na">class</span><span class="p">,</span>
    <span class="nx">\CodeIgniter\Validation\FileRules</span><span class="o">::</span><span class="na">class</span><span class="p">,</span>
    <span class="nx">\CodeIgniter\Validation\CreditCardRules</span><span class="o">::</span><span class="na">class</span><span class="p">,</span>
<span class="p">];</span>
</pre></div>
</div>
<p>You can add it as either a simple string with the fully qualified class name, or using the <code class="docutils literal notranslate"><span class="pre">::class</span></code> suffix as
shown above. The primary benefit here is that it provides some extra navigation capabilities in more advanced IDEs.</p>
<p>Within the file itself, each method is a rule and must accept a string as the first parameter, and must return
a boolean true or false value signifying true if it passed the test or false if it did not:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyRules</span>
<span class="p">{</span>
    <span class="k">public</span> <span class="k">function</span> <span class="nf">even</span><span class="p">(</span><span class="nx">string</span> <span class="nv">$str</span><span class="p">)</span><span class="o">:</span> <span class="nx">bool</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="p">(</span><span class="nx">int</span><span class="p">)</span><span class="nv">$str</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>By default, the system will look within <code class="docutils literal notranslate"><span class="pre">CodeIgniter\Language\en\Validation.php</span></code> for the language strings used
within errors. In custom rules, you may provide error messages by accepting a $error variable by reference in the
second parameter:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="k">function</span> <span class="nf">even</span><span class="p">(</span><span class="nx">string</span> <span class="nv">$str</span><span class="p">,</span> <span class="nx">string</span> <span class="o">&amp;</span><span class="nv">$error</span> <span class="o">=</span> <span class="k">null</span><span class="p">)</span><span class="o">:</span> <span class="nx">bool</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">((</span><span class="nx">int</span><span class="p">)</span><span class="nv">$str</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="nv">$error</span> <span class="o">=</span> <span class="nx">lang</span><span class="p">(</span><span class="s1">&#39;myerrors.evenError&#39;</span><span class="p">);</span>
        <span class="k">return</span> <span class="k">false</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="k">true</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Your new custom rule could now be used just like any other rule:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">validate</span><span class="p">(</span><span class="nv">$request</span><span class="p">,</span> <span class="p">[</span>
    <span class="s1">&#39;foo&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;required|even&#39;</span>
<span class="p">]);</span>
</pre></div>
</div>
<div class="section" id="allowing-parameters">
<h3><a class="toc-backref" href="#id27">Allowing Parameters</a><a class="headerlink" href="#allowing-parameters" title="Permalink to this headline">¶</a></h3>
<p>If your method needs to work with parameters, the function will need a minimum of three parameters: the string to validate,
the parameter string, and an array with all of the data that was submitted the form. The $data array is especially handy
for rules like <code class="docutils literal notranslate"><span class="pre">require_with</span></code> that needs to check the value of another submitted field to base its result on:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="k">public</span> <span class="k">function</span> <span class="nf">required_with</span><span class="p">(</span><span class="nv">$str</span><span class="p">,</span> <span class="nx">string</span> <span class="nv">$fields</span><span class="p">,</span> <span class="k">array</span> <span class="nv">$data</span><span class="p">)</span><span class="o">:</span> <span class="nx">bool</span>
<span class="p">{</span>
    <span class="nv">$fields</span> <span class="o">=</span> <span class="nb">explode</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">,</span> <span class="nv">$fields</span><span class="p">);</span>

    <span class="c1">// If the field is present we can safely assume that</span>
    <span class="c1">// the field is here, no matter whether the corresponding</span>
    <span class="c1">// search field is present or not.</span>
    <span class="nv">$present</span> <span class="o">=</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">required</span><span class="p">(</span><span class="nv">$str</span> <span class="o">??</span> <span class="s1">&#39;&#39;</span><span class="p">);</span>

    <span class="k">if</span> <span class="p">(</span><span class="nv">$present</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="k">true</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="c1">// Still here? Then we fail this test if</span>
    <span class="c1">// any of the fields are present in $data</span>
    <span class="c1">// as $fields is the lis</span>
    <span class="nv">$requiredFields</span> <span class="o">=</span> <span class="p">[];</span>

    <span class="k">foreach</span> <span class="p">(</span><span class="nv">$fields</span> <span class="k">as</span> <span class="nv">$field</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">array_key_exists</span><span class="p">(</span><span class="nv">$field</span><span class="p">,</span> <span class="nv">$data</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="nv">$requiredFields</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$field</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="c1">// Remove any keys with empty values since, that means they</span>
    <span class="c1">// weren&#39;t truly there, as far as this is concerned.</span>
    <span class="nv">$requiredFields</span> <span class="o">=</span> <span class="nb">array_filter</span><span class="p">(</span><span class="nv">$requiredFields</span><span class="p">,</span> <span class="k">function</span> <span class="p">(</span><span class="nv">$item</span><span class="p">)</span> <span class="k">use</span> <span class="p">(</span><span class="nv">$data</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="o">!</span> <span class="k">empty</span><span class="p">(</span><span class="nv">$data</span><span class="p">[</span><span class="nv">$item</span><span class="p">]);</span>
    <span class="p">});</span>

    <span class="k">return</span> <span class="k">empty</span><span class="p">(</span><span class="nv">$requiredFields</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Custom errors can be returned as the fourth parameter, just as described above.</p>
</div>
</div>
<div class="section" id="available-rules">
<h2><a class="toc-backref" href="#id28">Available Rules</a><a class="headerlink" href="#available-rules" title="Permalink to this headline">¶</a></h2>
<p>The following is a list of all the native rules that are available to use:</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Rule is a string; there must be <strong>no spaces</strong> between the parameters, especially the <code class="docutils literal notranslate"><span class="pre">is_unique</span></code> rule.
There can be no spaces before and after <code class="docutils literal notranslate"><span class="pre">ignore_value</span></code>.</p>
</div>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// is_unique[table.field,ignore_field,ignore_value]</span>

<span class="nv">$validation</span><span class="o">-&gt;</span><span class="na">setRules</span><span class="p">([</span>
    <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s2">&quot;is_unique[supplier.name,uuid, </span><span class="si">$uuid</span><span class="s2">]&quot;</span><span class="p">,</span>  <span class="c1">// is not ok</span>
    <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s2">&quot;is_unique[supplier.name,uuid,</span><span class="si">$uuid</span><span class="s2"> ]&quot;</span><span class="p">,</span>  <span class="c1">// is not ok</span>
    <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s2">&quot;is_unique[supplier.name,uuid,</span><span class="si">$uuid</span><span class="s2">]&quot;</span><span class="p">,</span>   <span class="c1">// is ok</span>
    <span class="s1">&#39;name&#39;</span> <span class="o">=&gt;</span> <span class="s2">&quot;is_unique[supplier.name,uuid,{uuid}]&quot;</span><span class="p">,</span>  <span class="c1">// is ok - see &quot;Validation Placeholders&quot;</span>
<span class="p">]);</span>
</pre></div>
</div>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="6%" />
<col width="52%" />
<col width="30%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Rule</th>
<th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Example</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>alpha</td>
<td>No</td>
<td>Fails if field has anything other than alphabetic characters.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>alpha_space</td>
<td>No</td>
<td>Fails if field contains anything other than alphabetic characters or spaces.</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>alpha_dash</td>
<td>No</td>
<td>Fails if field contains anything other than alphanumeric characters, underscores or dashes.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>alpha_numeric</td>
<td>No</td>
<td>Fails if field contains anything other than alphanumeric characters.</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>alpha_numeric_space</td>
<td>No</td>
<td>Fails if field contains anything other than alphanumeric or space characters.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>alpha_numeric_punct</td>
<td>No</td>
<td>Fails if field contains anything other than alphanumeric, space, or this limited set of
punctuation characters: ~ (tilde), ! (exclamation), # (number), $ (dollar), % (percent),
&amp; (ampersand), * (asterisk), - (dash), _ (underscore), + (plus), = (equals),
| (vertical bar), : (colon), . (period).</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>decimal</td>
<td>No</td>
<td>Fails if field contains anything other than a decimal number.
Also accepts a + or  - sign for the number.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>differs</td>
<td>Yes</td>
<td>Fails if field does not differ from the one in the parameter.</td>
<td>differs[field_name]</td>
</tr>
<tr class="row-even"><td>exact_length</td>
<td>Yes</td>
<td>Fails if field is not exactly the parameter value. One or more comma-separated values.</td>
<td>exact_length[5] or exact_length[5,8,12]</td>
</tr>
<tr class="row-odd"><td>greater_than</td>
<td>Yes</td>
<td>Fails if field is less than or equal to the parameter value or not numeric.</td>
<td>greater_than[8]</td>
</tr>
<tr class="row-even"><td>greater_than_equal_to</td>
<td>Yes</td>
<td>Fails if field is less than the parameter value, or not numeric.</td>
<td>greater_than_equal_to[5]</td>
</tr>
<tr class="row-odd"><td>hex</td>
<td>No</td>
<td>Fails if field contains anything other than hexadecimal characters.</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>if_exist</td>
<td>No</td>
<td>If this rule is present, validation will only return possible errors if the field key exists,
regardless of its value.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>in_list</td>
<td>Yes</td>
<td>Fails if field is not within a predetermined list.</td>
<td>in_list[red,blue,green]</td>
</tr>
<tr class="row-even"><td>integer</td>
<td>No</td>
<td>Fails if field contains anything other than an integer.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>is_natural</td>
<td>No</td>
<td>Fails if field contains anything other than a natural number: 0, 1, 2, 3, etc.</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>is_natural_no_zero</td>
<td>No</td>
<td>Fails if field contains anything other than a natural number, except zero: 1, 2, 3, etc.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>is_not_unique</td>
<td>Yes</td>
<td>Checks the database to see if the given value exist. Can ignore records by field/value to
filter (currently accept only one filter).</td>
<td>is_not_unique[table.field,where_field,where_value]</td>
</tr>
<tr class="row-even"><td>is_unique</td>
<td>Yes</td>
<td>Checks if this field value exists in the database. Optionally set a
column and value to ignore, useful when updating records to ignore itself.</td>
<td>is_unique[table.field,ignore_field,ignore_value]</td>
</tr>
<tr class="row-odd"><td>less_than</td>
<td>Yes</td>
<td>Fails if field is greater than or equal to the parameter value or not numeric.</td>
<td>less_than[8]</td>
</tr>
<tr class="row-even"><td>less_than_equal_to</td>
<td>Yes</td>
<td>Fails if field is greater than the parameter value or not numeric.</td>
<td>less_than_equal_to[8]</td>
</tr>
<tr class="row-odd"><td>matches</td>
<td>Yes</td>
<td>The value must match the value of the field in the parameter.</td>
<td>matches[field]</td>
</tr>
<tr class="row-even"><td>max_length</td>
<td>Yes</td>
<td>Fails if field is longer than the parameter value.</td>
<td>max_length[8]</td>
</tr>
<tr class="row-odd"><td>min_length</td>
<td>Yes</td>
<td>Fails if field is shorter than the parameter value.</td>
<td>min_length[3]</td>
</tr>
<tr class="row-even"><td>not_in_list</td>
<td>Yes</td>
<td>Fails if field is within a predetermined list.</td>
<td>not_in_list[red,blue,green]</td>
</tr>
<tr class="row-odd"><td>numeric</td>
<td>No</td>
<td>Fails if field contains anything other than numeric characters.</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>regex_match</td>
<td>Yes</td>
<td>Fails if field does not match the regular expression.</td>
<td>regex_match[/regex/]</td>
</tr>
<tr class="row-odd"><td>permit_empty</td>
<td>No</td>
<td>Allows the field to receive an empty array, empty string, null or false.</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>required</td>
<td>No</td>
<td>Fails if the field is an empty array, empty string, null or false.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>required_with</td>
<td>Yes</td>
<td>The field is required when any of the other required fields are present in the data.</td>
<td>required_with[field1,field2]</td>
</tr>
<tr class="row-even"><td>required_without</td>
<td>Yes</td>
<td>The field is required when all of the other fields are present in the data but not required.</td>
<td>required_without[field1,field2]</td>
</tr>
<tr class="row-odd"><td>string</td>
<td>No</td>
<td>A generic alternative to the alpha* rules that confirms the element is a string</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>timezone</td>
<td>No</td>
<td>Fails if field does match a timezone per <code class="docutils literal notranslate"><span class="pre">timezone_identifiers_list</span></code></td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>valid_base64</td>
<td>No</td>
<td>Fails if field contains anything other than valid Base64 characters.</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>valid_json</td>
<td>No</td>
<td>Fails if field does not contain a valid JSON string.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>valid_email</td>
<td>No</td>
<td>Fails if field does not contain a valid email address.</td>
<td>&#160;</td>
</tr>
<tr class="row-even"><td>valid_emails</td>
<td>No</td>
<td>Fails if any value provided in a comma separated list is not a valid email.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>valid_ip</td>
<td>No</td>
<td>Fails if the supplied IP is not valid. Accepts an optional parameter of ‘ipv4’ or
‘ipv6’ to specify an IP format.</td>
<td>valid_ip[ipv6]</td>
</tr>
<tr class="row-even"><td>valid_url</td>
<td>No</td>
<td>Fails if field does not contain a valid URL.</td>
<td>&#160;</td>
</tr>
<tr class="row-odd"><td>valid_date</td>
<td>No</td>
<td>Fails if field does not contain a valid date. Accepts an optional parameter
to matches a date format.</td>
<td>valid_date[d/m/Y]</td>
</tr>
<tr class="row-even"><td>valid_cc_number</td>
<td>Yes</td>
<td>Verifies that the credit card number matches the format used by the specified provider.
Current supported providers are: American Express (amex), China Unionpay (unionpay),
Diners Club CarteBlance (carteblanche), Diners Club (dinersclub), Discover Card (discover),
Interpayment (interpayment), JCB (jcb), Maestro (maestro), Dankort (dankort), NSPK MIR (mir),
Troy (troy), MasterCard (mastercard), Visa (visa), UATP (uatp), Verve (verve),
CIBC Convenience Card (cibc), Royal Bank of Canada Client Card (rbc),
TD Canada Trust Access Card (tdtrust), Scotiabank Scotia Card (scotia), BMO ABM Card (bmoabm),
HSBC Canada Card (hsbc)</td>
<td>valid_cc_number[amex]</td>
</tr>
</tbody>
</table>
<div class="section" id="rules-for-file-uploads">
<h3><a class="toc-backref" href="#id29">Rules for File Uploads</a><a class="headerlink" href="#rules-for-file-uploads" title="Permalink to this headline">¶</a></h3>
<p>These validation rules enable you to do the basic checks you might need to verify that uploaded files meet your business needs.
Since the value of a file upload HTML field doesn’t exist, and is stored in the $_FILES global, the name of the input field will
need to be used twice. Once to specify the field name as you would for any other rule, but again as the first parameter of all
file upload related rules:</p>
<div class="highlight-html+php notranslate"><div class="highlight"><pre><span></span><span class="c1">// In the HTML</span>
<span class="o">&lt;</span><span class="nx">input</span> <span class="nx">type</span><span class="o">=</span><span class="s2">&quot;file&quot;</span> <span class="nx">name</span><span class="o">=</span><span class="s2">&quot;avatar&quot;</span><span class="o">&gt;</span>

<span class="c1">// In the controller</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">validate</span><span class="p">([</span>
    <span class="s1">&#39;avatar&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;uploaded[avatar]|max_size[avatar,1024]&#39;</span>
<span class="p">]);</span>
</pre></div>
</div>
<table border="1" class="docutils">
<colgroup>
<col width="14%" />
<col width="7%" />
<col width="56%" />
<col width="24%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Rule</th>
<th class="head">Parameter</th>
<th class="head">Description</th>
<th class="head">Example</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>uploaded</td>
<td>Yes</td>
<td>Fails if the name of the parameter does not match the name of any uploaded files.</td>
<td>uploaded[field_name]</td>
</tr>
<tr class="row-odd"><td>max_size</td>
<td>Yes</td>
<td>Fails if the uploaded file named in the parameter is larger than the second parameter in
kilobytes (kb). Or if the file is larger than allowed maximum size declared in
php.ini config file - <code class="docutils literal notranslate"><span class="pre">upload_max_filesize</span></code> directive.</td>
<td>max_size[field_name,2048]</td>
</tr>
<tr class="row-even"><td>max_dims</td>
<td>Yes</td>
<td>Fails if the maximum width and height of an uploaded image exceed values. The first parameter
is the field name. The second is the width, and the third is the height. Will also fail if
the file cannot be determined to be an image.</td>
<td>max_dims[field_name,300,150]</td>
</tr>
<tr class="row-odd"><td>mime_in</td>
<td>Yes</td>
<td>Fails if the file’s mime type is not one listed in the parameters.</td>
<td>mime_in[field_name,image/png,image/jpg]</td>
</tr>
<tr class="row-even"><td>ext_in</td>
<td>Yes</td>
<td>Fails if the file’s extension is not one listed in the parameters.</td>
<td>ext_in[field_name,png,jpg,gif]</td>
</tr>
<tr class="row-odd"><td>is_image</td>
<td>Yes</td>
<td>Fails if the file cannot be determined to be an image based on the mime type.</td>
<td>is_image[field_name]</td>
</tr>
</tbody>
</table>
<p>The file validation rules apply for both single and multiple file uploads.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You can also use any native PHP functions that permit up
to two parameters, where at least one is required (to pass
the field data).</p>
</div>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="../helpers/index.html" class="btn btn-neutral float-right" title="Helpers" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="user_agent.html" class="btn btn-neutral float-left" title="User Agent Class" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2019-2021 CodeIgniter Foundation.
      <span class="lastupdated">
        Last updated on Feb 01, 2021.
      </span>

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(false);
      });
  </script>

  
  
    
   

</body>
</html>