Forms provide a way for users to interact with the application and submit data. 


What does the form validation in CodeIgniter:

  1. Check for required data.
  2. Verify that the data is of the correct type, and meets the correct criteria. 
  3. Sanitize the data for security.
  4. Pre-format the data if needed (Does the data need to be trimmed? HTML encoded? Etc.)
  5. Prepare the data for insertion in the database. 

CodeIgniter Validation Rules 

To set validation rules you will use the set_rules() method:

$this->form_validation->set_rules();

The above method takes three parameters as input:

  1. The field name - the exact name you’ve given the form field.
  2. A “human” name for this field, which will be inserted into the error message.
  3. The validation rules for this form field.

Here is an example. 

$this->form_validation->set_rules('username', 'Username', 'required');

Let's make the real example and understand validation

I am creating signup form where we will check the user data and validate it.


application/controllers/Signup.php

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Signup extends CI_Controller {

    public function __construct() {
        parent::__construct();

        //load the form_valiation lib you can also load from application/config/autoload.php
        $this->load->library('form_validation');
        $this->load->helper('url');
    }

    public function index() {

        $this->load->view("myform");
    }

    public function signup() {
       

        //set the validation rules
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');  // here "users" is database table name

        
        //if form is not validated return to myform
        if ($this->form_validation->run() == FALSE) {
            $this->load->view("myform");
        } else {
            
            echo "form has been validated";
           //now you can write query to insert data in database
            
           
        }
    }

}

application/views/myform.php

<head>
    <title>My Form</title>
</head>
<body>

    <?php echo validation_errors(); ?>

      <form action="<?php echo base_url();?>signup/signup" method="post">
        <h3>Signup form validation</h3>
        <p>Username</p>
        <input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />

        <p>Password</p>
        <input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />

        <p>Password Confirm</p>
        <input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" />

        <p>Email Address</p>
        <input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
        <div><input type="submit" value="Submit" /></div>
    </form>

</body>
</html>


Output:




Note: - always use the set_value()
run() - method only returns TRUE if it has successfully applied your rules without any of them failing.
set_value() - Update the value in each field 

The following is a list of all the native rules that are available to use:


Rule Parameter Description Example
required No Returns FALSE if the form element is empty.  
matches Yes Returns FALSE if the form element does not match the one in the parameter. matches[form_item]
regex_match Yes Returns FALSE if the form element does not match the regular expression. regex_match[/regex/]
differs Yes Returns FALSE if the form element does not differ from the one in the parameter. differs[form_item]
is_unique Yes Returns FALSE if the form element is not unique to the table and field name in the parameter. Note: This rule requires Query Builder to be enabled in order to work. is_unique[table.field]
min_length Yes Returns FALSE if the form element is shorter than the parameter value. min_length[3]
max_length Yes Returns FALSE if the form element is longer than the parameter value. max_length[12]
exact_length Yes Returns FALSE if the form element is not exactly the parameter value. exact_length[8]
greater_than Yes Returns FALSE if the form element is less than or equal to the parameter value or not numeric. greater_than[8]
greater_than_equal_to Yes Returns FALSE if the form element is less than the parameter value, or not numeric. greater_than_equal_to[8]
less_than Yes Returns FALSE if the form element is greater than or equal to the parameter value or not numeric. less_than[8]
less_than_equal_to Yes Returns FALSE if the form element is greater than the parameter value, or not numeric. less_than_equal_to[8]
in_list Yes Returns FALSE if the form element is not within a predetermined list. in_list[red,blue,green]
alpha No Returns FALSE if the form element contains anything other than alphabetical characters.  
alpha_numeric No Returns FALSE if the form element contains anything other than alpha-numeric characters.  
alpha_numeric_spaces No Returns FALSE if the form element contains anything other than alpha-numeric characters or spaces. Should be used after trim to avoid spaces at the beginning or end.  
alpha_dash No Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.  
numeric No Returns FALSE if the form element contains anything other than numeric characters.  
integer No Returns FALSE if the form element contains anything other than an integer.  
decimal No Returns FALSE if the form element contains anything other than a decimal number.  
is_natural No Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc.  
is_natural_no_zero No Returns FALSE if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc.  
valid_url No Returns FALSE if the form element does not contain a valid URL.  
valid_email No Returns FALSE if the form element does not contain a valid email address.  
valid_emails No Returns FALSE if any value provided in a comma separated list is not a valid email.  
valid_ip Yes Returns FALSE if the supplied IP address is not valid. Accepts an optional parameter of ‘ipv4’ or ‘ipv6’ to specify an IP format.  
valid_base64 No Returns FALSE if the supplied string contains anything other than valid Base64 characters.