Form Validation in CodeIgniter

A form is displayed to the user, who then fills it in and submits it. The Form Validation Library then checks the form against any rules that the developer has set. If an error occurs the library returns these errors and they are shown against the form with the fields re-populated. This process proceeds until a valid form is submitted.

The development process
You create a form, along with a dynamic value from a form helper function—this will re-populate the data if needed. You will also display individual or global errors in the form view file. You set validation rules, which must be adhered to. Then you check to see if the validation process has been run, and if it has not, you load the form view file.

Loading the assets

We need to load one libraries for form validation: the Form Validation Library. We can do this in one line

Setting the rules
The next step in using the Form Validation Library is to set the rules for the form. These rules are set and must be adhered to. The way we set rules is by using the
set_rules() function of the Form Validation Library. We use the function
as follows:

As you can see, the function accepts three parameters. The first is the name of the form field that you wish to set the rule for. The second parameter is the name that you wish to be assigned to this, for humans to read. The final parameter is where you pass any validation rules.

List of validation rules
The following rules are readily available for use:

  • required
  • matches[field_name]
  • min_length[x]
  • max_length[x]
  • exact_length[x]
  • alpha
  • alpha_numeric
  • alpha_dash
  • numeric
  • integer
  • is_natural
  • is_unique
  • is_natural_no_zero
  • valid_email
  • valid_emails
  • valid_ip
  • valid_base64
RuleParameterDescriptionExample
requiredNoReturns FALSE if the form element is empty.
matchesYesReturns FALSE if the form element does not match the one in the parameter.matches[form_item]
is_uniqueYesReturns FALSE if the form element is not unique to the table and field name in the parameter.is_unique[table.field]
min_lengthYesReturns FALSE if the form element is shorter then the parameter value.min_length[6]
max_lengthYesReturns FALSE if the form element is longer then the parameter value.max_length[12]
exact_lengthYesReturns FALSE if the form element is not exactly the parameter value.exact_length[8]
greater_thanYesReturns FALSE if the form element is less than the parameter value or not numeric.greater_than[8]
less_thanYesReturns FALSE if the form element is greater than the parameter value or not numeric.less_than[8]
alphaNoReturns FALSE if the form element contains anything other than alphabetical characters.
alpha_numericNoReturns FALSE if the form element contains anything other than alpha-numeric characters.
alpha_dashNoReturns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.
numericNoReturns FALSE if the form element contains anything other than numeric characters.
integerNoReturns FALSE if the form element contains anything other than an integer.
decimalNoReturns FALSE if the form element contains anything other than a decimal number.
is_naturalNoReturns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc.
is_natural_no_zeroNoReturns FALSE if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc.
valid_emailNoReturns FALSE if the form element does not contain a valid email address.
valid_emailsNoReturns FALSE if any value provided in a comma separated list is not a valid email.
valid_ipNoReturns FALSE if the supplied IP is not valid. Accepts an optional parameter of “IPv4” or “IPv6” to specify an IP format.
valid_base64NoReturns FALSE if the supplied string contains anything other than valid Base64 characters.

Source : https://www.codeigniter.com/userguide3/libraries/form_validation.html

As you can see, some of these rules have a single parameter.
The rule matches[] will return TRUE if the field matches the field name passed to it.
The min_length[], max_length[], and exact_length[] rules will take an integer as a parameter and check if the minimum length, maximum length respectively, or exact length matches the rule.
The rules with no parameters are pretty much self-explanatory. You are able to use more than one rule, simply separate rules with a vertical bar ‘|’ and they will cascade.

The rules
Now that we know how to set rules and what the rules we can use are, we can go ahead and set the rules necessary for our form. All fields should be required, and the e-mail field should be validated to ensure that the e-mail address is correctly formatted.

Check the validation process
Instead of checking one of the form field’s POST value to check if the form has been submitted, we simply check to see if the Form Validation Library has run. We do this by using the following code:

Example 1

Create a view signup_form view

Create signupform controller and don’t forget to load form_validation library

This method will show all errors at one place like this :

Form Validation Error1

If you want to print an error below partcular field then use form_error function.

Ex2 :

Signup_form view

Controller will be the same for both methods.

Output for Ex2 will be like this:

Form Validation Error2

View Demo 1 View Demo 2

CI Form Validation (Download script 1)
Size: 2.61 MB
Version: V 1.0

CI Form Validation (Download script 2)
Size: 2.61 MB
Version: V 1.)

Anuj Kumar

I'm Anuj kumar a web developer with 4+ year experience .I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.

Recommended Tutorials for you

You may also like...

3 Responses

  1. Can I follow the tutorial above on my website? I will include your website url.

    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *