Codeigniter Form Validation

Codeigniter Form Validation

Codeigniter Form Validation is a primary requirement of every project, so i will give you simple example of form validation in codeigniter 3 application. we will use form_validation library for add form validation with error message display in codeigniter.

In CodeIgniter, you can use the Form Validation Library to validate form inputs and display error messages if validation fails. Here’s an example that demonstrates how to set up form validation and display error messages. You Can Learn Codeigniter Get File Extension Before Upload Example

Steps For Codeigniter Form Validation

Codeigniter Form Validation Example

1. Controller Code:

First, load the form validation library and set validation rules in your controller.

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

class FormExample extends CI_Controller {

    public function index() {
        // Load form helper and validation library
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');

        // Set validation rules
        $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required|min_length[8]');

        // Run form validation
        if ($this->form_validation->run() == FALSE) {
            // Validation failed, load the form view
            $this->load->view('form_view');
        } else {
            // Validation successful, proceed further
            $this->load->view('form_success');
        }
    }
}

2. View Code (form_view.php):

In the view, display the form along with the validation error messages.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Validation Example</title>
</head>
<body>
    <h1>Form Validation Example</h1>

    <!-- Display error messages if validation fails -->
    <?php echo validation_errors(); ?>

    <!-- Form submission -->
    <?php echo form_open('formexample'); ?>
    
    <label for="username">Username:</label>
    <input type="text" name="username" value="<?php echo set_value('username'); ?>">
    <br><br>

    <label for="email">Email:</label>
    <input type="text" name="email" value="<?php echo set_value('email'); ?>">
    <br><br>

    <label for="password">Password:</label>
    <input type="password" name="password">
    <br><br>

    <input type="submit" value="Submit">
    <?php echo form_close(); ?>
</body>
</html>

3. Success View (form_success.php):

This view will be loaded when the form validation is successful.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Success</title>
</head>
<body>
    <h1>Form Submitted Successfully!</h1>
    <p>Your form has been validated and submitted successfully.</p>
</body>
</html>

4. Key Functions:

  • set_rules(): Defines validation rules for the input fields.
  • validation_errors(): Displays all validation error messages.
  • set_value(): Repopulates the form input fields if validation fails, preserving user data.

Validation Rules:

  • required: Ensures the field is not empty.
  • min_length[5]: Specifies the minimum length for a field (e.g., Username must be at least 5 characters).
  • valid_email: Validates that the email is in a proper format.

Running the Application:

  1. Navigate to the controller (e.g., http://localhost/your_project/formexample).
  2. Submit the form without filling it out or with incorrect data to see validation errors.
  3. If everything is correct, it will load the success page.

Leave a Reply