Displaying Validation Errors
When validation fails, Trongate stores errors in $_SESSION['form_submission_errors'] and provides four ways to display them.
The Four Display Methods
| Method | How to Use | Best For |
|---|---|---|
| General Errors | validation_errors() |
Simple forms, login pages, admin panels |
| Scoped Errors | validation_errors('FORM-login') |
Pages with more than one form |
| Inline Errors | validation_errors('field_name') |
Long forms, multi-step forms, better UX |
| JSON Errors | validation_errors(422) |
API endpoints, AJAX forms, mobile apps |
Method 1: General Errors (Display All)
Use with no arguments to display all validation errors at once, typically at the top of your form.
<h1><?= $headline ?></h1>
<?= validation_errors() ?>
<?php
echo form_open($form_location);
echo form_label('Email');
echo form_input('email', $email);
echo form_label('Password');
echo form_password('password');
echo form_submit('submit', 'Save');
echo form_close();
?>This approach is ideal for short forms where a single error summary is sufficient.
Customising Output
- Wrap each error:
View File
<?= validation_errors('<div class="error">', '</div>') ?> - Set global defaults:
PHP
define('ERROR_OPEN', '<div class="alert alert-danger">'); define('ERROR_CLOSE', '</div>'); - Wrap the full block:
View File
<div class="error-container"> <?= validation_errors() ?> </div>
Method 2: Scoped Errors (Form-Specific)
On pages with more than one form, give each form a form_name and render only that form's errors with the FORM- prefix. This prevents one form's errors from appearing under a different form.
<?php
echo form_open($login_location, ['form_name' => 'login']);
echo form_label('Email');
echo form_input('email', $email);
echo form_label('Password');
echo form_password('password');
echo form_submit('submit', 'Log In');
echo validation_errors('FORM-login'); // Login form's errors only
echo form_close();
// Second form on the same page...
echo form_open($contact_location, ['form_name' => 'contact']);
// ... contact fields ...
echo validation_errors('FORM-contact'); // Contact form's errors only
echo form_close();
?>Scoped rendering uses the default error wrappers. For custom wrappers plus scoping, use the position-3 spelling:
<?= validation_errors('</p>', '<p>', 'login') ?>Abstain without clearing: when the requested name does not match the recorded name (wrong name, or no name recorded), the scoped call renders nothing and does not clear the error bucket — the correct form's block still renders.
Footgun: a scoped call on a form that declared no form_name renders nothing, silently. Unnamed forms use the global spelling (validation_errors()).
POST-only scoping: the form name is read from the submitted POST data. GET forms never record a name, so scoped calls on GET forms always abstain.
Method 3: Inline Errors (Field-Specific)
Pass a field name to display errors next to the relevant input. This provides a more precise and user-friendly experience.
<?php
echo form_open($form_location);
echo form_label('Email Address');
echo validation_errors('email');
echo form_input('email', $email);
echo form_label('Password');
echo validation_errors('password');
echo form_password('password');
echo form_submit('submit', 'Register');
echo form_close();
?>This method is ideal for longer or more complex forms where users benefit from seeing exactly where problems occur.
Method 4: JSON Errors (APIs and AJAX)
Pass an HTTP status code (400–499) to return validation errors as JSON and immediately terminate execution.
public function api_create(): void {
$this->validation->set_rules('email', 'email address', 'required|valid_email');
$this->validation->set_rules('password', 'password', 'required|min_length[8]');
if ($this->validation->run() === false) {
validation_errors(422);
}
echo json_encode(['success' => true]);
}Example JSON response:
[
{
"field": "email",
"messages": ["The email address field is required."]
}
]Important: The JSON method terminates execution using exit(). No code after validation_errors(422) will run.
Automatic Error Highlighting
Add the highlight-errors class to your form to automatically highlight fields that failed validation.
<?php
echo form_open($form_location, ['class' => 'highlight-errors']);
echo form_label('Email');
echo form_input('email', $email);
echo form_label('Password');
echo form_password('password');
echo form_submit('submit', 'Login');
echo form_close();
?>When validation errors exist, injects JavaScript that applies the form-field-validation-error class to affected fields.
Choosing the Right Approach
- Simple forms: Use general errors
- Complex forms: Use inline errors
- Multi-form pages: Use scoped errors
- APIs: Use JSON responses
Best Practice: Choose one primary display method per form. Mixing approaches can lead to a confusing user experience.
Session Behaviour (Advanced)
Good to know: Validation errors are automatically cleared from the session after they are displayed.
In most cases, you don’t need to think about this - but for completeness:
validation_errors()displays all errors and clears themvalidation_errors('FORM-login')displays a named form's errors and clears them; on a name mismatch it renders nothing and leaves the bucket intactvalidation_errors('field')displays errors for one field and clears only that field’s errorsvalidation_errors(422)returns JSON, clears all errors, and terminates execution$this->validation->run()clears the bucket and recorded form name on a successful submission (clear-on-pass)form_close()does not clear validation errors — the summary call is safe anywhere: before the form, inside the block, or afterform_close()
We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.
Share your thoughts in the Documentation Feedback.