Codeigniter Form Validation with Error Message

Codeigniter Form Validation with Error Message

In this post we will give you information about Codeigniter Form Validation with Error Message. Hear we will give you detail about Codeigniter Form Validation with Error MessageAnd how to use it also give you demo for it if it is necessary.

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.']);

}

}

}

Also see:Codeigniter – How to get form post data in controller?

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 >

<h2>Codeigniter Form Validation with Error Message - ItSolutionStuff.com</h2>

<div style="display:none">

</div>

<?php echo validation_errors(); ?>

<form method="POST" action="/itemForm">

<div >

<label>First Name:</label>

<input type="text" name="first_name" placeholder="First Name">

</div>

<div >

<label>Last Name:</label>

<input type="text" name="last_name" placeholder="Last Name">

</div>

<div >

<strong>Email:</strong>

<input type="text" name="email" placeholder="Email">

</div>

<div >

<strong>Address:</strong>

<textarea name="address" placeholder="Address"></textarea>

</div>

<div >

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

Also see:Codeigniter – add/remove multiple input fields dynamically with jquery

http://localhost:8000/item/index

I hope it can help you…

Hope this code and post will helped you for implement Codeigniter Form Validation with Error Message. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

9  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US