Form Validation Library

The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time of adding validation to the form field.

Load the Form Validation Class

Before using the form validation class in Codeigniter, you must load in the controller file by following the syntax:

Class Reference

1. set_rules(): A set_rules() function is used to set the validation rule in the form fields.

 Syntax

 It has four parameters

$field (string): It defines the form’s fields name.

$label (string): In this parameter, you can define the label name of the form’s field. Eg. Username, Password, etc. 

$rules: As the parameter represents, it is used to set the validation rules as a string list separated by a pipe “|”, or an array.

$errors: In this field, you can define a list of custom error messages.

2. run(): The run() function is used to execute a group of validation rules in the form, and if the validation is failed, it returns FALSE.

Syntax

$group: It contains the name of the validation group to execute in a form.

3. set_message(): A set_message() function is used to set global or custom error messages in the form field.

Syntax

It has two parameters

$lang: In this field, you can declare the fieldname for which you want to set an error message.

$val: It defines the error message; you want to appear on the form.

Example: It defines an error message for a particular form’s fieldname.

To display the error message globally in a form.

4. reset_validation(): It allows you to reset the form validation field, when you validate an array more than once. It should be called before validation of the new array.

5. set_data(): It is used to validate an array of data that set in the set_data() function.

Syntax

$data: It defines an array of data to validate in set_data() function.

6. error_array(): It is used to display all the error messages in an array.

7. has_rule():  The has_rule() function is used to verify that a rule is set for the specified field. And if the rule is set for that field, it returns a true value; otherwise, it shows false.

Syntax

$field: It defines the name of the particular field to check the rule is present or not.

The Function of Form Validation

These are the following methods that are available in the form validation library for validating the form fields.

1. Setting validation rules in a form

A library allows you to set multiple validation rules in a given field. To set validation rules in a form field, you should use set_rules() method:

A set_rules() method contains three parameters as an input:

1. field name: The define field name should be unique and exactly the same as the form field.

2. Label name: A field name that you want to display in a form. Eg. Username, Email, etc.

3. And the last, Validation rule that is used for defining the rule of a particular field in a form.

4. (optional): It is an optional parameter to set custom error messages on any form fields. And if you don’t want to show any custom messages, it uses default error.

Example:

Create a program uses of validation rules

Create a controller file Valid_form.php and save it in application/controller/Valid_form.php. After that, write the following program in the controller file.

Valid_form.php

Create a view file view_form.php and save it in application/views/view_form.php. After that, write the following program in the controller file.

view_form.php

Create a view file success_form.php and save it in application/views/success_form.php. After that, write the following program in the controller file.

success_form.php

Now execute the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/Valid_form, it shows the output, as shown below.

Form Validation Library

Now submit the incomplete registration form to display the error messages, and it shows the output, as shown below.

Form Validation Library

Note: If you have filled the complete form and clicked on the submit button, it shows “Your form has been submitted successfully“.

2. Setting validation rules using an array

In the above method, we have used form validation rules to validate every field. Now in this method, we are setting the form validation rules using an array in action.

Syntax

Create a program uses of validation rules

Create a controller file Valid_form.php and save it in application/controller/Valid_form.php. After that, write the following program in the controller file.

Valid_form.php                    

Note: The view_form.php and success_form.php view files will remain the same in the views folder.

When you execute the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/Valid_form, it shows the output, as shown in the below image.

Form Validation Library

3. Cascading Rules

Using this rule, you can define multiple validation rules in each field of the form. It permits you to organize multiple rules using the pipe ( | ) symbol in the set_rules() function.

Syntax

Create a controller file Valid_form.php and save it in application/controller/Valid_form.php. After that, write the following program in the controller file.

Valid_form.php

Note: The view_form.php and success_form.php view files will remain the same in the views folder.

When you execute the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/valid_form controller, it shows the output, as shown below.

Form Validation Library

You have to fill all the details of the Registration form and click on the submit button.

After that, it validates all form fields according to set form validation rules, and shows the error message for each field, as shown in the images below.

Form Validation Library

4. Prepping Data

Preparing data methods is similar to cascading rules. It is used to check validation rules such as trimming fields, checking length, and verifying that both password fields must match.

For eaxmple,

5. Display errors on individual fields

If you want to display an error message for each field instead of displaying a list of errors, you can use the form_error() function.

Create a controller file Valid_form.php and save it in application/controller/Valid_form.php. After that, write the following program in the controller file.

Valid_form.php

Create a view file view_form.php and save it in application/views/view_form.php. After that, write the following program in the controller file.

view_form.php

Create a view file success_form.php and save it in application/views/success_form.php. After that, write the following program in the controller file.

success_form.php

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/valid_form controller; it shows the output, as shown below.

Form Validation Library

To show errors in individual fields, you must submit the form without filling the details in the registration form; it shows the output as shown below.

Form Validation Library

And if you have provided all the correct and complete details, it shows Your Registration form is successfully filled, as shown in the below image.

Form Validation Library

Pin It on Pinterest

Share This