Form Validation in CodeIgniter: The form_validation library
CodeIgniter is loaded with libraries and helpers. It has small Footprints but a very useful set of inbuilt stuff making the development quite easier. There is an inbuilt library available to perform Form Validation in CodeIgniter. With the help of this form validation library, you can validate any form very easily. Let’s know what “form validation” library in CodeIgniter is and how to validate a form in CodeIgniter using the form_validation library.
Validation is a significant step in building a web application that takes user data. It confirms that the data that we are getting from client-end is appropriate and valid to store and process. CodeIgniter form validation library makes this very easy. So, let’s see how we can validate a form in CodeIgniter.
Load the Form Validation Library
Before using any CodeIgniter library, we need to load that. To load any library we have two methods, i.e., either load in autoload, or load in controller whenever required by Ci_Loader.
The first method to use the Form Validation Library in your controller is to autoload Library by using code below in application/config/autoload.php file.
$autoload['libraries'] = array('form_validation');
Second way to use this library is by loading it in the controller where required. To do so, you need to put this code before validating your form.
$this->load->library('form_validation');
If you are using the first method, the library will always be available to use. However, by the second method, the library will be available only when y will load it.
Set Form Validation Rules
Every form field is different than the other hence each field needs a different or maybe some same validation. But you have to set rules for all the fields that you need to validate. You need to set the form validation rules in your controller, for this set_rules() function is used. Syntax to set Form Validation Rules
set_rules('element_name', 'Label', 'rule', ['Custom Error Messages']);
In this, there are four parameters, but the fourth parameter is optional.
The first parameter is ‘element_name’ where you need to give the field name that will be the same as the one in the name attribute of HTML form.
Second is Label that will be a Visible name for that field.
The third one is the ‘rule’ which specifies the validations to be applied to that particular field. For example: required, validate_email, etc.
Fourth is the last and optional one, in which you can enter the custom message for your error
// Your Controller
class user extends CI_controller(){
$this->load->library('form_validation');
$this->form_validation->set_error_delimitors('<div style="">', '</div>');
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
$this->form_validation->set_rules('email','Email','required|valid_email');
$this->form_validation->set_message(['required'=>'This Field is required', 'valid_email'=>'Enter Currect Email']);
if($this->form_validation->run() == False){
// Redirect to login page
}
}
Run CodeIgniter Form Validation
After setting up the rules, you need to run those validations to perform a check whether the form values are proper and correct or not. To do this, you need to use the run() function as I am using in the code below.
$this->form_validation->run();
In this way, validation for your form will run and if there is an error in the form then it returns False, true otherwise.
How to show error on HTML Form in CodeIgniter
Now we have set the rules for our form, but we have to learn how to show the error on the form when an error occurs, for this, we have to use validation_error () function.
// HTML Form
echo validation_errors(),
form_open('actionurl'),
form_input(['name'=>'username', 'type'=>'text', 'required'=>'']),
form_input(['name'=>'email', 'type'=>'email', 'required'=>'']),
form_password(['name'=>'password', 'type'=>'password', 'required'=>'']),
form_submit(['name'=>'submit', 'type'=>'submit']),
form_close();
In this way, you will have to apply echo form_validation->errors() to this function somewhere above or below the form which will show the errors.
Setting up custom Error Message in CodeIgniter Form validation
Often web developers want to keep their error messages custom, so for this, you can use the fourth parameter in the form element like in the code below.
$this->form_validation->set_rules ('email', 'Email', 'required | valid_email', ['required' => 'This field is required', 'valid_email' => 'Enter Currect Email']);
In this way, you can custom error messages of all validations by passing an array in the 4th parameter; if you do not give custom messages then the framework will show its default messages.
If you do not want to give a different error message in each element, then you can give an error message for all fields simultaneously using set_message() function.
For this you have to use set_message() function in Controller, we have done this implementation in Controller above, you can see. Now it will work on the entire form.
Set Error Delimiters
To show all errors on the form in Codeigniter The tag is used, but if you want, you can show the error in your favorite tag and in this way you can also change the CSS of the error text. The set_error_delimitors() function is used for this, you can see it in the Controller above, we have used it.
- Ancillary Classes: Use $this in Codeigniter helper or library
- What is cURL and how to use PHP cURL?
- MySQL Subqueries explained with examples
Error Validation in Codeigniter
In Codeigniter, many functions are given to validate the error, some of which are as follows –
is_unique [tblname.colname] – It is used instead of Ajax, it names the column with the name of the table and it checks in the column taking the data of the form to see if the value already exists or not.
matches [‘elemt_name’] – It is to name an element of the form and then check whether the value in the box is matching, it is very useful in place of password and confirms password
valid_email
valid_url
alpha,
numeric,
alpha_numeric,
alpha_numeric_space