Debugging CodeIgniter Form Validation: Why form_error() Appears Empty
When debugging a login form in CodeIgniter, it is common to encounter a scenario where the validation rules are configured correctly, yet the error messages fail to render. In the specific case of the provided controller and view, the developer has set up standard validation rules for the username, password, and position (jabatan). The logic dictates that if $this->form_validation->run() returns false, the view should reload. However, when the user submits the form with empty fields, the error cells remain blank.
The root cause lies in how CodeIgniter handles validation rule names versus field names, combined with the specific implementation of the callback function.
The Callback Rule Name Mismatch
The most critical issue in the controller is located within the login() method. Here, the developer sets a custom validation rule named 'login':
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[40]|xss_clean|callback_login');
This defines the callback function as login. However, when the login() method executes and returns false upon a failed authentication attempt, it attempts to set the error message using:
$this->form_validation->set_message('login', 'password salah');
In CodeIgniter's validation engine, the first argument of set_message() must match the rule name defined in set_rules(), not the field name. Since the rule name is 'callback_login' (derived from the function name passed to callback_...) or simply the string 'login' depending on how the framework resolves it, there is a high probability of a mismatch if the developer assumes the key is the field name (password).
More importantly, when a custom callback returns false, CodeIgniter automatically generates an error message based on the rule name. If you manually call set_message('login', ...) inside the callback function (or a separate method called by the controller), you must ensure that the key 'login' exactly matches the identifier used in set_rules. If the framework expects the error to be keyed under the field name (password) but the message is registered under the rule name (login), form_error('password') will not find anything, while form_error('login') might.
The run() Method and View Reloading
The controller logic uses an if/else block to determine whether to reload the login view or proceed to welcome:
if($this->form_validation->run() == false)
{
$this->load->view('login');
}
This is generally safe, but there is a subtle pitfall regarding session persistence. CodeIgniter's Form Validation library stores error data in the session. When run() returns false because of a failed callback (like callback_login), the errors are populated in the session array keyed by field name or rule name.
However, if the view is reloaded without passing the validation data to it (which happens implicitly via sessions), the errors should appear. If they do not, check the following:
- Rule Name Consistency: Ensure that
form_error('username'),form_error('password'), andform_error('jabatan')match the keys in$this->form_validation->set_rules(). - Callback Return Logic: The
login()method returnsfalsebut does not explicitly set a message for the specific rule that failed validation within the callback context unlessset_messageis called correctly inside the callback function itself. If the error is set in the controller after running the logic, it might be too late if the view has already been loaded.
Correcting the View Output
In the provided view code, the developer uses:
<td class="error"><?php echo form_error('username'); ?></td>
This is correct syntax for displaying field-specific errors. However, if the validation fails due to the custom login callback, the error might be stored under a different key depending on how CodeIgniter 1.x (which this syntax suggests) resolves the rule name.
To debug this further, you can iterate through all available errors in the session:
<?php echo form_error('', '<br>'); // Optional: generic error display ?>
Or, more robustly, check the validation array directly if accessible:
<?php foreach($this->form_validation->error_array() as $field => $errors):
<?php foreach($errors as $error): echo $error . '<br>'; ?>
</?php endforeach;
</?php>
If the errors appear here but not in the specific field cells, the issue is strictly a naming convention mismatch between set_rules and form_error.
Best Practice for Custom Callbacks
When writing custom validation callbacks in CodeIgniter, it is often cleaner to set the error message directly inside the callback function rather than returning false and setting the message later in the controller. This ensures the message is registered immediately when the rule fails.
function login($username, $password, $jabatan) {
// Perform authentication logic...
if ($value) {
return true;
} else {
// Set the error message directly inside the callback
// Ensure the first argument matches the rule name exactly
$this->form_validation->set_message('login', 'password salah');
return false;
}
}
By ensuring the rule name in set_rules aligns perfectly with the key used in set_message, and by handling the error registration within the callback, you guarantee that form_error() will retrieve and display the message correctly upon form reload.