codeigniter form validation error message does not display

codeigniter form validation error message does not display

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

CodeIgniter Form Validation Errors Disappear After Loading Models

When form validation fails in CodeIgniter, you expect error messages to appear on the view. However, if those errors vanish after loading a model and fetching data, the root cause is almost certainly an uninitialized session.

In your specific scenario, validation runs successfully without the model load block. Once you add $this->load->model('book_category') and $this->book_category->get_all_categories(), the error messages disappear. This behavior indicates that the validation array stored in the session is being cleared or overwritten during the model execution.

How CodeIgniter Handles Validation State

CodeIgniter 3 stores form validation state, including rules, run results, and error messages, in the session data under the key validation. When you call $this->form_validation->run(), it checks this session array. If validation fails, it populates the session with error messages.

However, the framework also provides a method called set_rules(). This method updates the validation rules and clears the existing validation data in the session if called after an initial run has occurred or if the session key is not properly preserved across logic blocks. More critically, loading a model via $this->load->model() triggers a specific internal process: it calls initialize() on the model class.

If your Book_Category model (or any other model loaded in that sequence) contains a method named __construct() or relies on an active session connection that inadvertently triggers a validation reset, you will lose the error state. But there is a more common culprit: the interaction between $this->load->model() and the validation instance itself.

The Hidden Reset Mechanism

The core issue lies in how CodeIgniter's Model class interacts with the session during initialization. When you load a model, the framework attempts to reset the global state to ensure a clean slate for that specific resource. In many configurations, this includes clearing the validation data array stored in the session if the validation instance is not explicitly preserved or if the model's constructor interacts with the session driver.

If your book_category model has any logic in its constructor that touches the database or session, it might inadvertently trigger a reset of the form validation state. Specifically, if the model uses $this->load->database() or similar calls within its constructor that change the active connection context, CodeIgniter's internal state management can sometimes flush the validation key from the session array before your view is rendered.

The Solution: Disable Model Auto-Load Resetting

To fix this without restructuring your entire application logic, you need to prevent the model loading process from clearing the validation state. You can do this by disabling the automatic reset of the validation data when loading models.

Add this configuration to your controller's constructor or before the model load line:

$config = array(
    'reset_validation' => FALSE,
);
$this->load->config('database'); // Ensure config is loaded if using legacy setup
// Or directly in the controller before loading the model:
$this->form_validation->set_rules(...); // Your existing rules
if ($this->form_validation->run() == FALSE) {
    // Prevent validation reset on model load
    $config = array('reset_validation' => FALSE);
    $this->load->model('book_category', '', $config); 
    // Note: Passing the third parameter to load->model is deprecated in CI3.2+
    
    // The reliable modern approach for CI3.2+:
    $this->load->model('Book_Category');
    // Ensure no other initialization clears the session
}

However, since passing configuration arrays to load->model() was removed in later CodeIgniter versions, the most robust solution is to ensure your model does not inadvertently reset the session. If you are using CodeIgniter 2.x (which uses $this->load->model), check your model's constructor. Ensure it does not call methods that might trigger a validation reset.

Alternative Approach: Manually Set Rules Again

If modifying the model or config is not feasible, you can bypass the session dependency by re-running the validation logic manually after loading the model. This ensures the error messages are regenerated based on the current input data rather than relying on the potentially cleared session state.

if ($this->form_validation->run() == FALSE) {
    $this->load->model('book_category');
    
    // Re-fetch validation errors to ensure they persist
    $errors = $this->form_validation->error_array();
    
    // Continue with your logic...
    $template_data['mainContentData']['book_categories'] = $this->book_category->get_all_categories();
}

Best Practice: Separate Data Loading from Validation Logic

While the above fixes the immediate issue, the architectural pattern suggests keeping data loading separate from validation checks. If you can move the model load into a dedicated method or ensure it only runs when necessary, you reduce the risk of state corruption.

Consider refactoring your controller to handle the "success" and "failure" paths distinctly:

public function add_subcategory() {
    $this->form_validation->set_rules('bookCategoryId', 'Book SubCategory Id', 'trim|required');
    $this->form_validation->set_rules('bookSubCategoryId', 'Book SubCategory Id', 'trim|required');
    $this->form_validation->set_rules('bookSubCategoryName', 'Book SubCategory Name', 'trim|required');

    if ($this->form_validation->run() == TRUE) {
        // Handle success
        $this->load->model('book_category');
        $this->book_category->save_data();
    } else {
        // Handle failure without loading heavy models if possible, 
        // or ensure the model load doesn't reset state
        $template_data['mainContentData']['book_categories'] = $this->book_category->get_all_categories();
        $template_data['mainContent'] = 'admin_add_book_subcategory';
        $this->template($template_data);
    }
}

By understanding that $this->load->model() can interfere with the validation session state, you can maintain error visibility even when fetching necessary dropdown data. If you are looking for modern migration paths or advanced form handling in PHP frameworks, resources like https://laravelcompany.com offer insights into how Laravel handles these state management issues differently, often by defaulting to a cleaner separation of concerns that avoids this specific pitfall.

Our network