display_errors()
public function display_errors(array $data = []): ?string
Description
Displays validation errors in one of three formats: general HTML list, inline field‑specific errors, or JSON API response. This method is the object‑oriented counterpart to the validation_errors() helper function.
The format is determined by the first_arg value in the $data array:
- null or omitted – General errors for all fields (wrapped in
<ul class="validation-errors">) - string (field name) – Inline errors for a specific field
- integer (400‑499) – JSON response with HTTP error code
Parameters
| Parameter | Type | Description |
|---|---|---|
| $data | array | Associative array with optional keys:
|
Return Value
| Type | Description |
|---|---|
| string|null |
|
Example Usage
PHP
// General errors (all fields)
$errors = $this->validation->display_errors();
// Returns: <ul class="validation-errors">...</ul> or null
// Inline errors for specific field
$email_errors = $this->validation->display_errors(['first_arg' => 'email']);
// Returns: <ul class="validation-errors validation-errors--inline">...</ul> or empty string
// JSON API response (terminates script)
$this->validation->display_errors(['first_arg' => 422]);
// Sends HTTP 422 with JSON error array, then exits
// Custom HTML wrapper
$errors = $this->validation->display_errors([
'first_arg' => '<div class="alert alert-danger">',
'closing_html' => '</div>'
]);
// Typical form controller pattern
public function submit() {
$this->validation->set_rules('email', 'Email', 'required|valid_email');
if (!$this->validation->run()) {
$data['errors'] = $this->validation->display_errors();
$this->view('form', $data);
return;
}
// Process valid data...
}Notes
- Errors are retrieved from
$_SESSION['form_submission_errors']. - In JSON mode (HTTP code), the script terminates after output – no return value.
- For inline errors, an empty string is returned if the field has no errors.
- After errors are displayed, they are cleared from session (to prevent duplicate display).
- Use the helper
validation_errors()for simpler, function‑based access.