Search

CodeIgniter  Form Validation

CodeIgniter Form Validation is an important process while building web application. It ensures that the data that we are getting is proper form data and valid to store in database or process. CodeIgniter has made this task very easy and relevant. Let us understand this process with a simple example.

  1. A View page containing a form.
  2. A View page containing a “success” message to be displayed upon successful submission.
  3. A Controller method to receive and process form validation the submitted form data.

The Form View 

Create a file form view validate-form.php and save the below code it in application/views/validate-form.php. This view page will display form where user can submit his word and we will validate form this page to ensure that it should not be empty while submitting.

<html>
   <head> 
      <title>CodeIgniter  Form Validation</title> 
   </head>
   <body>
         <?= form_open('form-validation'); ?>  
         <h5>Enter Some text</h5> 
         <input type="text" name="words" value="" />  
         <?= form_error('words') ?>
         <div><input type = "submit" value = "Submit" /></div>  
      	 <?= form_close() ?>
   </body>
</html>

The Success Page

Using a text editor, create a form called form-success.php. In it, place this code and save it to your application/views/ folder:

<html>
    <head>
	<title>Form validation success</title>
    </head>
<body>
    <h3>Your form was successfully submitted!</h3>
</body>
</html>

The Controller

Using a text editor, create a controller called Form.php. In it, place this code and save it to your application/controllers/ folder:

<?php
class Form extends CI_Controller {
       public function index(){
        $this->load->helper(array('form', 'url'));
	    $this->load->library('form_validation');
		/* Set validation rule for name field in the form */ 
         $this->form_validation->set_rules('words', 'Words', 'required'); 
		if ($this->form_validation->run() == FALSE){
                      $this->load->view('validate-form');
                }else{
                      $this->load->view('form-success');
                }
        }
}

Add the following code in application/config/routes.php 

$route['form-validation'] = 'Form';

Let us execute this example by visiting the following URL in the browser. This URL may be different based on your side but User interface is same .

CodeIgniter Form Validation Rules 

We are describe some important rules following table

RuleParameterDescriptionExample
requiredNoThis rule return FALSE if your data is empty$this->form_validation->set_rules('fieldname', 'field label', 'required');
min_lengthYESThis rule return FALSE if the form element is shorter than the parameter value.$this->form_validation->set_rules('fieldname', 'field label', 'min_length[4]');
max_lengthYESThis rule return FALSE if the form element is max than the parameter value.$this->form_validation->set_rules('fieldname', 'field label', 'max_length[4]');
decimalNOThis rule return FALSE if the form element contains anything other than a decimal number.$this->form_validation->set_rules('fieldname', 'field label', 'decimal');
integerNOThis rule return FALSE if the form element contains anything other than a integer number.$this->form_validation->set_rules('fieldname', 'field label', 'integer');
numericNOThis rule return FALSE if the form element contains anything other than numeric characters.$this->form_validation->set_rules('fieldname', 'field label', 'numeric');
valid_emailNOThis rule return FALSE if the form element does not contain a valid email address.$this->form_validation->set_rules('fieldname', 'field label', 'required');

If you want to know more rules than you can visit the official website of CodeIgniter https://codeigniter.com/userguide3/libraries/form_validation.html

Stack Overlode is optimized Tutorials and examples for learning and training. Examples might be simplified to improve reading and learning and understand. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using Stack Overlode, you agree to have read and accepted our terms of use, cookie and privacy policy.