CodeIgniter Form Validation

Installing CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter which is 3.1.7 that I've used in this tutorial. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as codeigniter_validation so I'm using the below code.
  1. localhost/codeigniter_validation
codeigniter successful install

Configuring our Base URL

Next, we configure our base url to tell codeigniter that this is the URL of our site/application. We're gonna be using this a lot that's why we need to configure this. 1. In your codeigniter app folder, open config.php located in application/config folder. 2. Find and edit the ff line:
  1. $config['base_url'] = 'http://localhost/codeigniter_validation';

Creating our Form

Next we create the form that we are going to validate. Create the ff files inside application/views folder. myform.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeIgniter Form Validation</title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. <style type="text/css">
  8. label{
  9. margin-top:7px;
  10. }
  11. .alert{
  12. margin-top:20px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="container">
  18. <div class="row">
  19. <h1 class="page-header text-center">CodeIgniter Form Validation</h1>
  20. <div class="col-sm-6 col-sm-offset-3">
  21. <?php
  22. if(validation_errors()){
  23. ?>
  24. <div class="alert alert-danger text-center">
  25. <?php echo validation_errors(); ?>
  26. </div>
  27. <?php
  28. }
  29.  
  30. if($this->session->flashdata('success')){
  31. ?>
  32. <div class="alert alert-success text-center">
  33. <span class="glyphicon glyphicon-check"></span> <?php echo $this->session->flashdata('success'); ?>
  34. </div>
  35. <?php
  36. }
  37. ?>
  38. <div class="well">
  39. <form method="POST" name="validation" action="<?php echo base_url(); ?>index.php/form/validate">
  40. <div class="form-group row">
  41. <div class="col-sm-2">
  42. <label class="text-center">Firstname:</label>
  43. </div>
  44. <div class="col-sm-10">
  45. <input type="text" class="form-control" value="<?php echo set_value('firstname'); ?>" name="firstname">
  46. </div>
  47. </div>
  48. <div class="form-group row">
  49. <div class="col-sm-2">
  50. <label class="text-center">Lastname:</label>
  51. </div>
  52. <div class="col-sm-10">
  53. <input type="text" class="form-control" value="<?php echo set_value('lastname'); ?>" name="lastname">
  54. </div>
  55. </div>
  56. <div class="form-group row">
  57. <div class="col-sm-2">
  58. <label class="text-center">Email:</label>
  59. </div>
  60. <div class="col-sm-10">
  61. <input type="text" class="form-control" value="<?php echo set_value('email'); ?>" name="email">
  62. </div>
  63. </div>
  64. <div class="form-group row">
  65. <div class="col-sm-2">
  66. <label class="text-center">Contact:</label>
  67. </div>
  68. <div class="col-sm-10">
  69. <input type="text" class="form-control" value="<?php echo set_value('contact'); ?>" name="contact">
  70. </div>
  71. </div>
  72. <div class="form-group row">
  73. <div class="col-sm-2">
  74. <label class="text-center">Username:</label>
  75. </div>
  76. <div class="col-sm-10">
  77. <input type="text" class="form-control" value="<?php echo set_value('username'); ?>" name="username">
  78. </div>
  79. </div>
  80. <div class="form-group row">
  81. <div class="col-sm-2">
  82. <label class="text-center">Password:</label>
  83. </div>
  84. <div class="col-sm-10">
  85. <input type="password" class="form-control" value="<?php echo set_value('password'); ?>" name="password">
  86. </div>
  87. </div>
  88. <div class="form-group row">
  89. <div class="col-sm-2">
  90. <label class="text-center">Re-Type:</label>
  91. </div>
  92. <div class="col-sm-10">
  93. <input type="password" class="form-control" value="<?php echo set_value('confirm'); ?>" name="confirm">
  94. </div>
  95. </div>
  96. <hr>
  97. <button type="submit" class="btn btn-success">Validate</button>
  98. </form>
  99. </div>
  100. </div>
  101. </div>
  102. </div>
  103. </body>
  104. </html>

Defining our Default Controller

Next, we are going to set our default controller so that if no controller is defined, this default controller will be used. Open routes.php located in application/config folder and set the default route to our user controller.
  1. $route['default_controller'] = 'form';

Creating our Controller

Lastly, we are going to create our controller that will handle our validation. Create a file named Form.php in application/controllers folder of our app and put the ff codes.
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Form extends CI_Controller {
  5.  
  6. function __construct(){
  7. parent::__construct();
  8. $this->load->helper(array('url', 'form'));
  9. $this->load->library('form_validation');
  10. }
  11.  
  12. public function index(){
  13. $this->load->library('session');
  14. $this->load->view('myform');
  15. }
  16.  
  17. public function validate(){
  18. $this->load->library('session');
  19.  
  20. $this->form_validation->set_rules('firstname', 'Firstname', 'required|alpha');
  21. $this->form_validation->set_rules('lastname', 'Lastname', 'required|alpha');
  22. $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
  23. $this->form_validation->set_rules('contact', 'Contact', 'required|numeric');
  24. $this->form_validation->set_rules('username', 'Username', 'required|min_length[7]|max_length[30]');
  25. $this->form_validation->set_rules('password', 'Password', 'required|min_length[7]|max_length[30]');
  26. $this->form_validation->set_rules('confirm', 'Re-type Password', 'required|matches[password]');
  27.  
  28. if ($this->form_validation->run() == FALSE) {
  29. $this->load->view('myform');
  30. }
  31.  
  32. else{
  33. $this->session->set_flashdata('success','Form Validated');
  34. $this->load->view('myform');
  35. }
  36. }
  37. }
Validations used in this tutorial
  • required - returns false if the form field is empty
  • valid_email - returns false if the form field is ot a valid email
  • numeric - returns false if the form field contains non-numeric values
  • alpha - return false if the form field contains a non-alphabetic values
  • min_length[param] - returns false if the form field is lesser than the param value
  • max_length[param] - returns false if the form field is greater than the param value
  • matches[fieldname] - returns false if the form field does not much the set fieldname
That ends this tutorial. Happy Coding :)

Add new comment