Displaying Validation Errors and Handling Form Submissions in CodeIgniter

One of the critical aspects of web development is ensuring that user input is valid and handled properly. CodeIgniter, a popular PHP framework, provides built-in functions and libraries to validate form submissions and display helpful error messages. In this article, we will explore the process of displaying validation errors and handling form submissions in CodeIgniter.

Setting Up a Form

Before diving into the validation and submission handling, let's quickly set up a form in CodeIgniter. Assume we have a 'Contact Us' page that allows users to submit their details, including name, email, and message. Create a new view file named 'contact_form.php' and add the following code:

<?php echo form_open('contact/submit'); ?>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" required>

    <label for="email">Email:</label>
    <input type="email" name="email" id="email" required>

    <label for="message">Message:</label>
    <textarea name="message" id="message" required></textarea>

    <input type="submit" value="Submit">
</form>

In this form, we use the form_open() function to generate the necessary HTML markup for our form and specify the 'contact/submit' URL as the form's action attribute. The input fields are marked as required using the required attribute.

Validation Rules

Next, let's define the validation rules for handling the form submission in CodeIgniter. Create a new file named 'Contact.php' under the 'controllers' directory and add the following code:

<?php
class Contact extends CI_Controller {
    public function submit() {
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('message', 'Message', 'required');

        if ($this->form_validation->run() === FALSE) {
            $this->load->view('contact_form');
        } else {
            // Handle successful form submission
            // e.g., send the email or save the data
        }
    }
}

In the 'submit' method, we first load the 'form_validation' library using $this->load->library('form_validation');. Then, we set the validation rules for each input field using the set_rules() method. In our example, the 'name' and 'message' fields are set as 'required', and the 'email' field requires a valid email format.

Displaying Validation Errors

Now, let's modify our 'contact_form.php' view to display the validation errors. Update the form code as follows:

<?php echo form_open('contact/submit'); ?>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" required>
    <?php echo form_error('name'); ?>

    <label for="email">Email:</label>
    <input type="email" name="email" id="email" required>
    <?php echo form_error('email'); ?>

    <label for="message">Message:</label>
    <textarea name="message" id="message" required></textarea>
    <?php echo form_error('message'); ?>

    <input type="submit" value="Submit">
</form>

In the updated view, we use the form_error() function to display any validation error messages associated with the respective input field. For example, <?php echo form_error('name'); ?> will display the validation error message for the 'name' input field.

Handling Form Submission

Lastly, let's handle the successful form submission in our 'Contact' controller's 'submit' method:

// ...

if ($this->form_validation->run() === FALSE) {
    $this->load->view('contact_form');
} else {
    // Handle successful form submission
    $name = $this->input->post('name');
    $email = $this->input->post('email');
    $message = $this->input->post('message');

    // Perform required actions, such as sending email or saving data

    $this->load->view('success_message'); // Display a success message view
}

// ...

In this section, we retrieve the form inputs using $this->input->post() and perform the necessary actions, such as sending an email or saving the data to a database. Finally, we load a view named 'success_message.php' to display a success message confirming the form submission.

Congratulations! You have learned how to display validation errors and handle form submissions in CodeIgniter. By following these guidelines, you can ensure that user input is validated and processed correctly for a better user experience. Happy coding!


noob to master © copyleft