validation_errors()
function validation_errors(string|int|null $first_arg = null, string $closing_html = ''): ?string
Quick Start: Use validation_errors() for general form errors, validation_errors('fieldname') for inline errors, or validation_errors(422) for JSON API responses.
Description
Displays validation error messages stored in the session. This flexible function supports three output modes: general error display, inline field-specific errors, and JSON error responses for APIs.
Parameters
| Parameter | Type | Description |
|---|---|---|
$first_arg |
string|int|null |
(optional) This overloaded argument determines the function's mode:
|
$closing_html |
string | (optional) The closing HTML tag when $first_arg is used as an opening HTML tag. Defaults to an empty string (''). |
Return Value
| Type | Description |
|---|---|
| string|null | Formatted validation errors as HTML or JSON (for API mode), or null if no errors exist. |
Session Management: Errors are automatically cleared from the session after display. Each error set displays only once per request.
Mode 1: General Error Display (All Errors)
Display all validation errors at once, typically at the top of a form. By default, errors are wrapped in <p style="color: red;"> tags unless ERROR_OPEN and ERROR_CLOSE constants are defined globally.
The Email field is required.
The Password must be at least 8 characters long.
Custom HTML Wrappers (Mode 1 Custom)
You can override the default wrappers by passing custom HTML tags:
Mode 2: Inline Field Errors (Field-Specific)
Display errors next to specific form fields by passing the field name (string) as the first argument. Errors are automatically wrapped in a <div class="validation-error-report"> container.
Mode 3: JSON Error Responses (API)
Return errors as structured JSON for API endpoints by passing an HTTP status code (integer between 400 and 499) as the first argument.
JSON Mode Important: When using JSON mode, the function sends headers, outputs the JSON, and exits execution. No further code runs after validation_errors(422).
JSON Output Format (Example)
Global Error Styling
Define default error wrappers in your configuration to avoid passing them as arguments every time:
Prerequisite: Errors must first be set using Trongate's Validation class. The validation_errors() function only displays errors that have already been stored in the session.
Automatic Field Highlighting
The framework automatically injects JavaScript to highlight fields that have validation errors. To enable this behavior, simply add class="highlight-errors" to your form's opening tag:
Common Patterns Summary
| Use Case | Code | Mode | Result |
|---|---|---|---|
| Simple form errors | validation_errors() |
Mode 1 (Default) | All errors in default <p> format |
| Styled general errors | validation_errors('<div class="error">', '</div>') |
Mode 1 (Custom) | Each error wrapped individually |
| Field-specific error | validation_errors('email') |
Mode 2 | Only email field errors, wrapped in validation-error-report |
| API response | validation_errors(422) |
Mode 3 | JSON with 422 status code and script exit |