Codeigniter Form Validation with Error Message
Form Validation is a primary requirement of every project, so i will give you simple example of form validation in codeigniter 3 application. we will use form_validation library for add form validation with error message display in codeigniter.
we can use default following validation rules of codeigniter 3:
1) required
2) regex_match
3) matches
4) differs
5) is_unique
6) min_length
7) max_length
8) exact_length
9) greater_than
10) greater_than_equal_to
11) less_than
12) less_than_equal_to
13) in_list
14) alpha
15) alpha_numeric
16) You can see more from here Codeigniter 3 Validation Rules.
So here i gave you full example of form validation in codeigniter application. i created simple form with first name, last name, email and address like contact us form and i set server side validation. So let's simple see bellow step and make it form validation quick.
Step 1: Create Routes
In first step we require to add two route for display form and post form. so open routes.php file and add code like as bellow:
application/config/routes.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['item'] = "item/index";
$route['itemForm'] = "item/itemForm";
Step 2: Create Item Controller
In this step, we need to create Item Controller and define two methods, index() and itemForm(), Here we will also load form_validation and session library. So let's proceed.
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
In last step, we need to create view file, So here create item.php view file and copy bellow code, here we will display form.
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 with Error Message - ItSolutionStuff.com</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>
Now we are ready to this full example.
So let's run bellow command on your root directory for quick run:
php -S localhost:8000
Now you can open bellow URL on your browser:
http://localhost:8000/item/index
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Codeigniter Compress Image Size Example
- Codeigniter Upload Multiple File and Image Example
- How to Use Sweet Alert for Delete Confirm in Codeigniter?
- How to Create a Custom Library in CodeIgniter?
- Codeigniter Crop Image Before Upload Example
- Codeigniter Restful API Tutorial Example
- How to Get form Post Data in Controller Codeigniter?
- Codeigniter Google Recaptcha Form Validation Example
- Codeigniter Ajax Pagination using JQuery Example
- Codeigniter Delete Multiple Rows using Checkbox Example
- How to Create Dynamic Sitemap in Codeigniter?
- How to Get and Set Config Variables in Codeigniter?
- Codeigniter Drag and Drop Multiple Image Upload Example
- Codeigniter Select2 Ajax Autocomplete from Database Example