Introduction to Codeigniter Form Validation
In this post i am going to discuss about the various methods of using the codeigniter validation library.
Lets start by doing a simple example.
Here is our form for this example. This form is contained in the view file.
First thing we have to do to use the validation library is to load the library. Validation library should be loaded in the constructer of the controller class you want.
load->library('validation'); } } ?>
After loading the library we can use validation functions of the library inside this controller class. Example is given below.
validation->set_error_delimiters('', '
'); // Validation rules $rules['username']= "trim|required"; $rules['password']= "trim|required"; $this->validation->set_rules($rules); $fields['username'] = 'My Username'; $fields['password'] = 'My Password'; $this->validation->set_fields($fields); // Validate input data if ($this->validation->run() == FALSE) { $this->message = $this->validation->error_string; }else { // Proceed with other methods in the function } } ?>
In the above code sample inside the login method we have specified the error delimeters as
. That means when ever error is generated the each error message will be displayed inside tag.
Next we have to specify the validation rules for the fields. We set validation fields in an array called $rules. username and password are the input field names. Validation rules have to be given one by one seperated by | in a string. In this example i have used trim and required validators. These two are built in validators in Code Igniter.
Set the rule names in $rulearray and set to validation object using followiing code.
$this->validation->set_rules($rules);
Then we have to specify the display name of the error message. Set the display names in $field array and set to validation object using followiing code.
$this->validation->set_fields($fields);
If we dont specify it the system will automatically use the field name as display name of the error. Here i have given ‘My Username’ and ‘My Password’ as the display values. So if we keep the fields empty and click submit button following error messages will be generated.
My Username is required
My Password is required
If we dont specify the display names error messages will be like the following.
username is required
password is required
Then we call the run method to start validation. If there are errors in the input values for the given fields, validation object will return an error string and we have to return it to the view using
$this->message = $this->validation->error_string;
Otherwise we can continue to call other function in the process.
What happens inside Validation Library
When we call the run method in the validation library it will automatically split the rules string in to validation rules. Then the function executes the validation methods one by one.
Example :
// Cycle through the rules and test for errors foreach ($this->_rules as $field => $rules) { //Explode out the rules! $ex = explode('|', $rules); }
Here the loop runs two times for username and password. First it expldes and splits rules of usernames into an array called $ex.Then it takes rules one by one and executes it.
Once a rule is identified it will automatically take the specified parametr values from $_POST array and validates against the rule.
Code Igniter Validation Library Rules
Rule | Description |
required | Returns FALSE if the form element is empty. |
matches | Returns FALSE if the form element does not match the one in the parameter. matches[form_item] |
min_length | Returns FALSE if the form element is shorter then the parameter value. min_length[6] |
max_length | Returns FALSE if the form element is longer then the parameter value. max_length[12] |
exact_length | Returns FALSE if the form element is not exactly the parameter value. exact_length[8] |
alpha | Returns FALSE if the form element contains anything other than alphabetical characters. |
alpha_numeric | Returns FALSE if the form element contains anything other than alpha-numeric characters. |
alpha_dash | Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes. |
numeric | Returns FALSE if the form element contains anything other than numeric characters. |
integer | Returns FALSE if the form element contains anything other than an integer. |
valid_email | Returns FALSE if the form element does not contain a valid email address. |
valid_emails | Returns FALSE if any value provided in a comma separated list is not a valid email. |
valid_ip | Returns FALSE if the supplied IP is not valid. |
valid_base64 | Returns FALSE if the supplied string contains anything other than valid Base64 characters. |
Populate the Form Data After Validation
After submitiing the form if it contains any errors we need to display error messages to the user as well as keep the values of the fields which do not have any errors. We can do this by using set_value method.
Username />
Codeigniter Custom Validations Rules
In the next post about validation in CodeIgniter i am going to show you how to create Custom validation classes and custom rules. Date range validations, multiple select box validation , percentage , between and many more rules will be provided. So feel free to comment about your ideas and requirements relating to this topic.
Leave a Reply