Table of contents
1.
Introduction
2.
CodeIgniter
3.
Form Validation
4.
Form Validation Tutorial
4.1.
From
4.2.
Success Page
4.3.
Controller
4.4.
Try 
5.
Setting Validation Rules
6.
Frequently Asked Questions
6.1.
What is form helper in CodeIgniter?
6.2.
Is CodeIgniter front end or back end?
6.3.
Is CodeIgniter an OOP?
6.4.
What is payload validation?
6.5.
What are the three types of data validation?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Codeigniter form validation

Author Muskan Sharma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Readers!!

We all fill out forms for various purposes.

But have you ever thought about how complex the code behind this is?

For this, we have CodeIgniter. It helps the developer to have a minimized code.

Let's begin to learn more about this.

Codeigniter form validation

CodeIgniter

It is a PHP MVC framework used to create web apps quickly. It provides various libraries which help in reducing the complex code.

Recommended Topic, Interpolation in Angular

Form Validation

It is one of the libraries provided by CodeIgniter. Let's take a simple example. We come across various forms to fill in the information; to build that form, very complex code is written. But with the help of Form Validation, we can minimize the code.

Now let's begin the implementation of form validation.

Form Validation Tutorial

Before starting the implementation, we require three things:

  • A View file with a form in it.
  • A View file with a "success" message will be shown upon successful submission.
  • A  controller method for receiving and handling the data submissions.

From

Create a form called myInfo.php with the help of a text editor and save it to apps/Views/Folders.

<html>
<head>
<title>My Info </title>
</head>
<body>
<?php echo validation_errors(); ?>
<?php echo form_open('form'); ?>
<h5>Username</h5>
<input type="text" name="username" value="" size="60" />
<h5>Password</h5>
<input type="text" name="password" value="" size="60" />
<h5>Password Confirm</h5>
<input type="text" name="passconfirm" value="" size="60" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="60" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>

Success Page

Create a form called successform.php with the help of a text editor and save it to apps/Views/Folders.

<html>
<head>
<title>My Info</title>
</head>
<body>
<h3>Yay!! Your form was successfully submitted!</h3>
<p><?php echo anchor('form', 'Please Try it again!'); ?></p>
</body>
</html>

Controller

Create a Controller called Controllerform.php with the help of a text editor and save it to apps/Controller/Folder.

<?php
class Form extends CI_Controller {
        public function index()
        {
                $this->load->helper(array('form', 'url'));

                $this->load->library('form_validation');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('myInfo');
                }
                else
                {
                        $this->load->view('successform');
                }
        }
}

Try 

To try the form, visit your site. The form should reload after you submit it because you haven't yet established any validation rules.

So, let's set the validation rules.

Setting Validation Rules

In Codeigniter, you can set any number of Validation rules, and to set the validation rules use the set_rules() method.

$this->form_validation->set_rules();

The set_rules() method takes three parameters, which are given below:

  • Field name-The exact name you provide for the form field is the field name.
  • A name that the human in the error message easily understands.
  • The validation rules for the form field.

Example:

Add this to your Controller

$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconfirm', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');

Now your Controller will look like this:

<?php
class Form extends CI_Controller {
        public function index()
        {
                $this->load->helper(array('form', 'url'));
                $this->load->library('form_validation');
                $this->form_validation->set_rules('username', 'Username', 'required');
                $this->form_validation->set_rules('password', 'Password', 'required',
                        array('required' => 'You must provide a %s.')
                );
                $this->form_validation->set_rules('passconfirm', 'Password Confirmation', 'required');
                $this->form_validation->set_rules('email', 'Email', 'required');


                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('myInfo');
                }
                else
                {
                        $this->load->view('successform');
                }
        }
}


Must Read: Laravel Facades

Frequently Asked Questions

What is form helper in CodeIgniter?

It builds an opening form tag with a base URL based on your configuration preferences.

Is CodeIgniter front end or back end?

CodeIgniter is the front-end development.

Is CodeIgniter an OOP?

Yes, CodeIgniter is Object Oriented Programming.

What is payload validation?

The Validate Payload Node allows a workflow to validate all or a portion of the current payload against a JSON schema and branch based on the results.

What are the three types of data validation?

The three types of data validations are: Code Check, Range Check, and Format Check.

Conclusion

This blog has extensively discussed CodeIgniter form validation. This article helped enhance your knowledge about the form Validation Tutorial and setting the Validation rules for the form.

If you want to learn more deeply, check out the excellent content on the Coding Ninjas Website:

Yii FrameworkLaravel EloquentLaravel Facades

Refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. 

Refer to the links problemstop 100 SQL problemsresources, and mock tests to enhance your knowledge.

For placement preparations, visit interview experiences and interview bundle.

Thank You

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass