Forms Validation in CodeIgniter Framework


In CodeIgniter Project, Open autoload.php file in config folder. Add form and url helper to helper config as below:

$autoload['helper'] = array('form', 'url');

In CodeIgniter Project, Open autoload.php file in config folder. Add form_validation library to libraries config as below:

$autoload['libraries'] = array('form_validation');

In CodeIgniter Project, Open config.php file in config folder. Set value for base_url config as below:

$config['base_url'] = 'http://localhost:9092/LearnCodeIgniterWithRealApps/';




Create new PHP file named demo.php in controllers folder as below:

<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Demo extends CI_Controller
{

    public function index()
    {
        $this->load->view('demo/index');
    }

    function save()
    {
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]|max_length[12]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|max_length[20]|regex_match[/^((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})$/]');
        $this->form_validation->set_rules('repassword', 'Re-Password', 'trim|required|min_length[3]|max_length[20]|matches[password]');
        $this->form_validation->set_rules('age', 'Age', 'trim|required|integer|greater_than[18]|less_than[120]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('demo/index');
        } else {
            $data['account'] = $_POST;
            $this->load->view('demo/success', $data);
        }
    }
}

In CodeIgniter Project, Open routes.php file in config folder. Set value for default_controller as below:

$route['default_controller'] = 'demo';

Create new folder named demo in views folder. In this folder, create new views as below:

Create new PHP file named index.php as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
	<head>
		<title>Demo Page</title>
		<style type="text/css">
		  .error {
		      color: red;
		  }
		</style>
	</head>
	<body>

		<h3>Register</h3>

		<?php echo validation_errors('<p class="error">', '</p>'); ?>

		<?php echo form_open('demo/save'); ?>
        	<table cellpadding="2" cellspacing="2">
        		<tr>
        			<td>Username</td>
        			<td>
        				<input type="text" name="username" value="<?php echo set_value('username'); ?>"/>
        			</td>
        			<td><?php echo form_error('username', '<span class="error">', '</span>'); ?></td>
        		</tr>
        		<tr>
        			<td>Password</td>
        			<td>
        				<input type="password" name="password" value="<?php echo set_value('password'); ?>"/>
        			</td>
        			<td><?php echo form_error('password', '<span class="error">', '</span>'); ?></td>
        		</tr>
        		<tr>
        			<td>Re-Password</td>
        			<td>
        				<input type="password" name="repassword" value="<?php echo set_value('repassword'); ?>"/>
        			</td>
        			<td><?php echo form_error('repassword', '<span class="error">', '</span>'); ?></td>
        		</tr>
        		<tr>
        			<td>Age</td>
        			<td>
        				<input type="text" name="age" value="<?php echo set_value('age'); ?>"/>
        			</td>
        			<td><?php echo form_error('age', '<span class="error">', '</span>'); ?></td>
        		</tr>
        		<tr>
        			<td>Email</td>
        			<td>
        				<input type="text" name="email" value="<?php echo set_value('email'); ?>"/>
        			</td>
        			<td><?php echo form_error('email', '<span class="error">', '</span>'); ?></td>
        		</tr>
        		<tr>
        			<td>&nbsp;</td>
        			<td>
        				<input type="submit" value="Save"/>
        			</td>
        		</tr>
        	</table>
		<?php echo form_close(); ?>

	</body>
</html>

Create new PHP file named success.php as below:

<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<html>
    <head>
        <title>Success Page</title>
    </head>
    <body>

    	<h3>Account Info</h3>

    	<table cellpadding="2" cellspacing="2" border="1">
    		<tr>
    			<td>Username</td>
    			<td><?php echo $account['username']; ?></td>
    		</tr>
    		<tr>
    			<td>Password</td>
    			<td><?php echo $account['password']; ?></td>
    		</tr>
    		<tr>
    			<td>Age</td>
    			<td><?php echo $account['age']; ?></td>
    		</tr>
    		<tr>
    			<td>Email</td>
    			<td><?php echo $account['email']; ?></td>
    		</tr>
    	</table>

    </body>
</html>




Access index action in Demo controller with following url: http://localhost:9092/LearnCodeIgniterWithRealApps/demo/index

Output

Click Save button submit form to Save action in Demo controller with invalid data as below:

Click Save button submit form to Save action in Demo controller with valid data

Output

I recommend you refer to the books below to learn more about the knowledge in this article: