Form validation is a fundamental element of every project, so I will give you a simple example of form validation in Codeigniter 3. We will use the form_validation library to add form validation with an error message in Codeigniter.



Follow the following steps to understand form validation:


Step 1: Create Routes

set route in application/config/routes.php

<?php

$route['item'] = "item/index";
$route['itemForm'] = "item/itemForm";


Step 2: Create Item Controller

We need to create Item Controller and define two methods, index() and itemForm()


application/controllers/Item.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
  
class Item extends CI_Controller {
   
   /**
    * Get All Data from this method.
    *
    * @return Response
   */
   public function __construct() {
      parent::__construct(); 
  
      $this->load->library('form_validation');
      $this->load->library('session');
   }
   
   /**
    * Create from display on this method.
    *
    * @return Response
   */
   public function index()
   {
      $this->load->view('item');
   }
   
   /**
    * Store Data from this method.
    *
    * @return Response
   */
   public function itemForm()
   {
        $this->form_validation->set_rules('first_name', 'First Name', 'required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('address', 'Address', 'required');
   
        if ($this->form_validation->run() == FALSE){
            $this->load->view('item'); 
        }else{
           echo json_encode(['success'=>'Record added successfully.']);
        }
    }
}


Step 3: Create View File

Create view file, item.php


application/views/item.php

<!DOCTYPE html>
<html>
<head>
    <title>Codeigniter Ajax Validation Example</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
</head>
<body>
   
<div class="container">
    <h2>Codeigniter Form Validation</h2>
   
    <div class="alert alert-danger print-error-msg" style="display:none">
    </div>
   
    <?php echo validation_errors(); ?>
   
    <form method="POST" action="/itemForm">
        <div class="form-group">
            <label>First Name:</label>
            <input type="text" name="first_name" class="form-control" placeholder="First Name">
        </div>
   
        <div class="form-group">
            <label>Last Name:</label>
            <input type="text" name="last_name" class="form-control" placeholder="Last Name">
        </div>
   
        <div class="form-group">
            <strong>Email:</strong>
            <input type="text" name="email" class="form-control" placeholder="Email">
        </div>
   
        <div class="form-group">
            <strong>Address:</strong>
            <textarea class="form-control" name="address" placeholder="Address"></textarea>
        </div>
  
        <div class="form-group">
            <button class="btn btn-success btn-submit">Submit</button>
        </div>
    </form>
</div>
  
</body>
</html>


Lets run our application and the output

http://localhost/your_project_name/item/index


Thanks, I hope it will work for you..