MilkCore

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
example
// Add error messages
MessagesHandler::add_error('Invalid email format', 'email');
MessagesHandler::add_error('Please fill out all required fields');

// Add success message
MessagesHandler::add_success('Your profile has been updated successfully');

// Mark a field as invalid without a specific message
MessagesHandler::add_field_error('password');

// Display messages in a template
$errors = MessagesHandler::get_error_messages();
$success = MessagesHandler::get_success_messages();

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
example
// Add a general error message
MessagesHandler::add_error('An unexpected error occurred');

// Add a field-specific error message
MessagesHandler::add_error('Email is already in use', 'email');

// Add an error for multiple fields
MessagesHandler::add_error('Passwords do not match', ['password', 'confirm_password']);

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
example
// Mark a field as invalid
MessagesHandler::add_field_error('username');

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
example
MessagesHandler::add_success('Your changes have been saved');

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
example
// In a template file
MessagesHandler::display_messages();

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
example
// Get errors as a string with HTML breaks
$errorString = MessagesHandler::errors_to_string(true);
echo $errorString;

// Get errors as a plain text string
$plainErrors = MessagesHandler::errors_to_string();
mail('admin@example.com', 'Form Errors', $plainErrors);
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
example
// Get and display error messages
$errorHtml = MessagesHandler::get_error_alert();
if (!empty($errorHtml)) {
    echo $errorHtml;
}
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
example
// Get all error messages
$errors = MessagesHandler::get_errors();
foreach ($errors as $field => $message) {
    echo "Error in {$field}: {$message}\n";
}
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
example
// In a form template
$invalidClass = MessagesHandler::get_invalid_class('email');
echo '<input type="email" name="email" class="form-control ' . $invalidClass . '">';
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
example
// Get and display success messages
$successHtml = MessagesHandler::get_success_alert();
if (!empty($successHtml)) {
    echo $successHtml;
}
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
example
// Get all success messages
$successMessages = MessagesHandler::get_success_messages();
foreach ($successMessages as $message) {
    echo "Success: {$message}\n";
}
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
example
if (MessagesHandler::has_errors()) {
    // Don't proceed with form processing
    return;
}
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
example
// Reset all error messages before new validation
MessagesHandler::reset();

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
example
// Get success messages as a string with HTML breaks
$successString = MessagesHandler::success_to_string(true);
echo $successString;
Return values
string

All success messages combined into a single string


        
On this page

Search results