MessagesHandler
in package
Message Handling System
This class manages various types of messages throughout the application, including form validation errors, success notifications, and field-specific validation errors. It provides a centralized way to collect and display messages to users.
Tags
Table of Contents
Methods
- add_error() : void
- Adds an error message
- add_field_error() : void
- Marks a field as invalid without adding a specific error message
- add_success() : void
- Adds a success message
- display_messages() : void
- Outputs error and success messages
- errors_to_string() : string
- Converts all error messages to a single string
- get_error_alert() : string
- Gets HTML for error messages in an alert box
- get_errors() : array<string|int, mixed>
- Gets all error messages
- get_invalid_class() : string
- Gets the CSS class for invalid fields
- get_success_alert() : string
- Gets HTML for success messages in an alert box
- get_success_messages() : array<string|int, mixed>
- Gets all success messages
- has_errors() : bool
- Checks if there are any error messages or invalid fields
- reset() : void
- Resets all error messages and invalid fields
- success_to_string() : string
- Converts all success messages to a single string
Methods
add_error()
Adds an error message
public
static add_error(string $message[, mixed $field = '' ]) : void
This method adds an error message to the collection. If a field name is provided, the message is associated with that field and the field is marked as invalid.
Parameters
- $message : string
-
The error message to display
- $field : mixed = ''
-
Field name or array of field names to associate with the error
Tags
add_field_error()
Marks a field as invalid without adding a specific error message
public
static add_field_error(string $field) : void
This method is useful when you want to highlight a field as invalid but the error message is handled elsewhere or is not needed.
Parameters
- $field : string
-
The name of the field to mark as invalid
Tags
add_success()
Adds a success message
public
static add_success(string $message) : void
This method adds a success message to be displayed to the user.
Parameters
- $message : string
-
The success message to display
Tags
display_messages()
Outputs error and success messages
public
static display_messages() : void
This method directly outputs HTML for both error and success messages. It's typically used in templates to display all messages at once.
Tags
errors_to_string()
Converts all error messages to a single string
public
static errors_to_string([bool $br = false ]) : string
This method combines all error messages into a single string, with either HTML line breaks or newlines between messages.
Parameters
- $br : bool = false
-
Whether to use HTML line breaks (true) or newlines (false)
Tags
Return values
string —All error messages combined into a single string
get_error_alert()
Gets HTML for error messages in an alert box
public
static get_error_alert() : string
This method generates a Bootstrap alert box containing all error messages. Each error is wrapped in a div with a data-field attribute for JavaScript targeting.
Tags
Return values
string —HTML for the error alert box or an empty string if there are no errors
get_errors()
Gets all error messages
public
static get_errors() : array<string|int, mixed>
This method returns the array of all error messages, which can be used for custom error handling or display.
Tags
Return values
array<string|int, mixed> —Array of error messages
get_invalid_class()
Gets the CSS class for invalid fields
public
static get_invalid_class(string $field_name) : string
This method returns the appropriate CSS class for form fields that have validation errors. It's typically used in templates to highlight invalid fields.
Parameters
- $field_name : string
-
The name of the field to check
Tags
Return values
string —The CSS class for invalid fields or an empty string if the field is valid
get_success_alert()
Gets HTML for success messages in an alert box
public
static get_success_alert() : string
This method generates a Bootstrap alert box containing all success messages. Each message is wrapped in a div for consistent styling.
Tags
Return values
string —HTML for the success alert box or an empty string if there are no success messages
get_success_messages()
Gets all success messages
public
static get_success_messages() : array<string|int, mixed>
This method returns the array of all success messages, which can be used for custom success handling or display.
Tags
Return values
array<string|int, mixed> —Array of success messages
has_errors()
Checks if there are any error messages or invalid fields
public
static has_errors() : bool
This method determines if there are any validation errors that need to be displayed.
Tags
Return values
bool —True if there are error messages or invalid fields, false otherwise
reset()
Resets all error messages and invalid fields
public
static reset() : void
This method clears all error messages and invalid field markers. It's typically used when you want to start fresh with a new validation.
Tags
success_to_string()
Converts all success messages to a single string
public
static success_to_string([bool $br = false ]) : string
This method combines all success messages into a single string, with either HTML line breaks or newlines between messages.
Parameters
- $br : bool = false
-
Whether to use HTML line breaks (true) or newlines (false)
Tags
Return values
string —All success messages combined into a single string