Site icon WDB24

Codeigniter Form Validation Tutorial Example – with error message and callback

codeigniter featured image

In this tutorial, we will see how to add server-side validation with custom error messages and callback to the login and registration using Codeigniter form validation class. I will create 3 forms (2 logins, 1 registration) and add validation on each of the forms.

My assumption of this tutorial is that you already installed and configured codeigniter in localhost environment. If you are facing any problem on configuring codeigniter you can follow my post how to install codeigniter in xampp.

Further I am not going to add any data in mysql database. I will make form validation only. If you want to know learn login and registration with database interaction, you can follow my below posts.

How form validation works:

So you learn the basic rule. Let’s start by taking basic login form example. I will be creating a view simple_login.php in application/views folder.

Example 1) Basic Login Form:

Basic Login Form View:

Below is the basic bootstrap 4 login form that has POST method and will be posted on the same url. There is also validation_errors and $this->session->flashdata('success') which I will discuss later.

Let’s create a Forms controller and put basic code

Forms Controller:

Below is the controller class with 2 method __construct() and simple_login_form(). In __construct() method I have loaded mandatory classes and helper functions.

simple_login_form() method will load the simple_login.php view and form on simple_login.php is also submit on this method.
$this->form_validation->set_rules() will check the rules for required input fields. Rules in email fields are 'trim|required|valid_email' which means data should first be trim and it is required and it should be valid email address.

$this->form_validation->run() will run the validation. If validation return FALSE then I will show same login form with error messages otherwise I will show success messages.

$this->input->post() get post data using field name. $this->session->set_flashdata() is used to set temporary data. In our case I will show success messages

Error Message(s):

validation_errors() function will display all the validation errors. This function also takes opening and closing tags for any custom html styling. But you can also set styling in controller.

Success Message:

$this->session->flashdata('success') has success message and I am checking if it is set then show it with in success div.

Example 2) Advance Login Form:

Advance Login Form View:

Advance login form is just like simple login form except it has set_value() method in email input field. Set_value() function is retain the posted value and display the value within the text box using input name.

Forms Controller (advance_login_form) method:

advance_login_form() method display advance login form and form will also be submitted on this function. I split form rules into an array and provide separate rules for both fields.

Email validation rules has callback function email_check which will be checking that email must be valid gmail address. Remember callback function must have callback prefix.

$this->form_validation->set_error_delimiters() will use to set error delimiters. In a simple login form example, I set this in the view.

I use second parameter as TRUE in $this->input->post() which means that form data must XSS clean.

email_check() method is a callback function which will make sure that email provided by user must be valid gmail address. It also set custom message for invalid email address.

Example 3) Registration Form:

Below is the registration form built with Bootstrap 4. It has basic registration fields with mostly all the stuff which I already mentioned in previous 2 examples.

Forms Controller (registration_form) method:

Below code is almost the same as advance login form example. Only difference is rules for validation. In email validation I use is_unique[users.email], users is a database table and email is table column name which means is_unique will check the email column of users table and if email is already present in user table it through an error but remember this rule require Query Builder enable in order to work.
In the next argument I also provide custom message for is_unique which is “Email address is already registered”.

Password has min_length[8]|alpha_numeric' rules which will be checking password should be minimum 8 digit of length and it must be in alpha numeric format.

Success and error messages same as shown in above example.

So let’s wrap this is up. Now you the basic knowledge of codeigniter form validation. We have seen some basic and some advanced form validation. We also apply the different style of writing validation rules.

If you wish to know more about Codeigniter form validation you can visit https://www.codeigniter.com/userguide3/libraries/form_validation.html

Exit mobile version