HDTuto.com

Form Validation Error Messages in Codeigniter



Here, i want to show you how to display form validation error message in codeigniter project. we will use codeigniter 3 server side form validation and display error messages on front side. you can follow this article and you can learn to implement codeigniter form validation error message display.

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 will help you...




August 15, 2019 | Category : Codeigniter
Related Posts



Blog Search