How to do form validation in CodeIgniter 3

If you are developing a form for the user, you must do the form validation after user submits the form. You can perform a very simple to use form validation in CodeIgniter 3. In this topic, we will create a simple registration form in CodeIgniter 3 for candidate registration as an example. Our main purpose is to know how you can validate the form in CodeIgniter 3. Form validation will be performed after the form is submitted and before data is inserted into the database table.

Step 1 - Create a MySQL table for candidate details

Let us create the table named 'candidates' in MySQL database. I have a database named 'demo'. So, this table will be created in demo database. If you have an existing database other than demo you can also use it. Just make sure same table does not exist already. Table structures is given below:

Table: candidates

This table stores details of all candidates who completed successful registration.

CodeIgniter Form Validation with Form Submit Example

Below are the columns in this table.

  1. candidate_id - It is the primary key and auto incremented id of the candidate
  2. name - Name of the candidate
  3. email_id - Email Id of the candidate
  4. address - Address of the candidate
  5. age - Age of the candidate
Create table script for this table are given below; you can use this code to create the table.

candidates.sql

After you create the table, verify if table structure is same as above.

Step 2 - Create the view for registration form (register.php)

It is a simple registration form with input fields name, address, email id and age. There will be validation for each field. For age I will be using a range (18-40) so that I can show how this type of validation can be done in CodeIgniter 3. Let us see the form below:

application/views/register.php

It is just a form with few input fields and a submit button. Note that for each input field set_value() will repopulate the value in case of validation error for the field . Also, for each input field, validation error message is displayed using form_error() in case validation fails for a field.

Step 3 - Write Controller code (CandidateController.php)

Our controller will have an index() method to display the form and a register() method to process the submitted form. register() method is the one where form validation will take place. We will have an additional method which is for the age validation callback function. Let's see the code below for the controller class.

application/controllers/CandidateController.php

In the constructor, I am loading the model named 'ModelCandidate'.

Controller Method index()

index() method is executed by default in a controller. In this method it just loads the view that displays the registration form.

Controller Method register()

This method gets executed afetr form submission. It writes validation rules for each field. If validation is unsuccessful for a field, it reloads the view with error message displayed. But if all fields are validated successfully, this method calls a Model method to insert a row in database table.

It uses form_validation->set_rules() method for each field. Validation for the Age field (callback method) is done using a custom callback method named age_check() which is defined later in the controller. This callback method returns TRUE or FALSE. Let's see the callback method age_check() below:

It takes the value of the field as parameter and checks if the value is less than 18 or greater than 40. If the value is outside this range, it sets a message and returns FALSE. Otherwise, it returns TRUE.

So, this was our controller, now let's see the Model code in next step.

Step 4 - Write Model code (ModelCandidate.php)

In the model, register() method takes the input array and uses $db->insert() to insert a row in the databasse table. Below is the model code:

application/models/ModelCandidate.php

Our Model is also developed. Now Let me give you the stylesheet I am using below:

style.css

I just added few simple styles here, you can always add more and better styles.

Step 5 - Update Configuration files and Test the Application

Let us now update few setup files before we run the application. Before that, let me show you the folder structure I am using. I have created a folder named 'registration' under xampp/htdocs and kept all my files in this folder. So, the root directory is xampp/htdocs/registration.

Create registration form in CodeIgniter

The folder 'assets' is created to keep all stylesheets, JavaScripts and image files. Here I have style.css which is kept in assets/css folder. Just make sure you mention the correct path for css file in the view. For our case it is added in the view register.php as below:

Create Registration Form using CodeIgniter and MySQL

We will update below files in config folder and also, we will have .htaccess file in the root directory. See below:

Learn Codeigniter Form Validation in Simple Steps

  1. Update autoload.php (application/config/autoload.php)

    I have added 'database' and 'form_validation' in autoload libraries.

    Also, 'url' and 'form' are added in helper array.

  2. Update config file (application/config/config.php)

    Set 'base_url' and 'index_page' as below in this file:

  3. Update database setup file (application/config/database.php)

    As I mentioned before, I am using a database named 'demo' in localhost with user 'root' with no password, so I updated database.php as below:

  4. Update routes file (application/config/routes.php) I updated routes.php for default controller which is candidate:
  5. Update .htaccess (hypertext access) file to remove index.ph from the URL

    .htaccess file is used to do configuration changes for a directory. I have kept .htaccess file in my root folder which is xampp/htdocs/registration.

    form validation set rules in codeigniter

    Using URL rewriting rule, it redirects all request except those which are not existing files or directories.

Test The Application

Make sure in your XAMPP control panel Apache web server and MySQL services are running. Open the browser and run localhost/registration. You will see the registration form displayed as below:

codeigniter form validation example

You can test the validation and successful registration. Just try to submit incomplete form and see the error messages. Do not forget to test age validation.

When you enter all valid values and submit, registartion should be completed successfully and you can check with the database table if the row is inserted in it.

Our development and testing are done. Hope you could understand all the steps and could test it successfully.

How to validate a form in CodeIgniterDownload Source Code

I have put all the codes for controller, model and view in a zip file. You can download it by clicking on the Download button below. You do not need to register yourself to download it. You can use the code as is or you can modify them as per your requirements.

How to validate a form in CodeIgniter 3Conclusion

Form validation is a very important part of any project. Hope it was useful for you to validate the form using CodeIgniter.