0.0.5 โ€ข Published 3 months ago

@solvia/ng-message-handler v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

Error Handler Library

A lightweight, flexible error handling library for Angular applications that provides standardized validation error display and management.


Table of Contents


Installation

npm install @solvia/ng-message-handler

๐Ÿ“š Overview

This library provides a comprehensive solution for handling and displaying form validation errors in Angular applications. It includes:

  • โœ… A reusable error message component that integrates with Angular's form system
  • ๐Ÿ› ๏ธ A centralized service for managing error messages
  • โš™๏ธ A provider function for configuring global error messages
  • ๐Ÿงพ Default error messages for common validation scenarios

๐Ÿงฉ Components

LmnErrorMessageComponent

LmnErrorMessageComponent is a standalone component that displays form validation errors. It implements ControlValueAccessor to seamlessly integrate with Angular's reactive and template-driven forms.

๐Ÿ”‘ Key Features

  • ๐Ÿ”— Automatic binding to parent form controls
  • ๐Ÿ“ข Displays the first validation error for a form control
  • ๐Ÿ“ Supports custom error messages per component instance
  • ๐ŸŽ›๏ธ Configurable display conditions based on touched or dirty states
  • ๐ŸŽจ Easy styling with standard CSS class application

๐Ÿ› ๏ธ Services

ErrorMessageProviderService

ErrorMessageProviderService is an injectable service that provides and manages global error messages used across the application. It allows registering custom error messages and retrieving them dynamically at runtime.

๐Ÿ”‘ Key Features

  • ๐Ÿงฉ Centralized management of all error messages
  • ๐Ÿ—ฃ๏ธ Support for both string-based and function-based error messages
  • ๐Ÿงฌ Flexible override capabilities:
    • Globally (application-wide)
    • Per component instance

โš™๏ธ Configuration

provideErrorHandling

The provideErrorHandling function sets up the error handling system at the application level.
It can be used in the providers array of your app's bootstrap configuration to register custom error messages during application startup.

๐Ÿ“ฆ Usage Example

import { provideErrorHandling } from '@solvia/ng-message-handler';

bootstrapApplication(AppComponent, {
  providers: [
    provideErrorHandling({
      required: 'This field is required',
      minlength: (err) => `Minimum ${err.requiredLength} characters required`
    })
  ]
});

๐Ÿš€ Usage Examples

โœ… Basic Usage

Here's a simple example of how to use the LmnErrorMessageComponent within a reactive form.

import { Component } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
import { LmnErrorMessageComponent } from '@solvia/ng-message-handler';

@Component({
  selector: 'app-registration',
  standalone: true,
  imports: [ReactiveFormsModule, LmnErrorMessageComponent],
  template: `
    <form [formGroup]="form">
      <div>
        <label for="email">Email</label>
        <input id="email" formControlName="email" type="email">
        <lmn-error-message controlName="email"></lmn-error-message>
      </div>

      <div>
        <label for="password">Password</label>
        <input id="password" formControlName="password" type="password">
        <lmn-error-message controlName="password"></lmn-error-message>
      </div>
    </form>
  `
})
export class RegistrationComponent {
  form = this.fb.group({
    email: ['', [Validators.required, Validators.email]],
    password: ['', [Validators.required, Validators.minLength(8)]]
  });

  constructor(private fb: FormBuilder) {}
}

๐ŸŽจ Component-Specific Custom Messages

You can define custom error messages at the component level using the customMessages input on the LmnErrorMessageComponent.

<lmn-error-message 
  controlName="email" 
  [customMessages]="{
    required: 'Email is required for account creation',
    email: 'Please enter a valid email format (e.g., user@example.com)'
  }">
</lmn-error-message>

๐ŸŽจ Custom CSS Styling

The component makes styling errors simple by applying CSS classes to the error message container. You can style errors using standard CSS:

/* In your global styles or component styles */
.error-message {
  color: #d32f2f;
  font-size: 12px;
  margin-top: 4px;
  font-weight: 500;
}

๐ŸŽจ Custom Styling with Class Attribute

For custom styling, simply use the class attribute:

<lmn-error-message 
  controlName="email" 
  class="error-message">
</lmn-error-message>

๐Ÿ“˜ API Reference

LmnErrorMessageComponent

A standalone component that displays form validation errors.

Selector: lmn-error-message


๐Ÿ”ง Inputs

NameTypeDefaultDescription
customMessagesErrorMessages{}Custom error messages specific to this component instance
showOnlyWhenDirtybooleanfalseWhether to show errors only when the field has been modified
controlNamestringundefinedThe name of the form control to bind to
debugbooleanfalseWhether to output debug information to the console

๐Ÿงฉ HTML Attributes

AttributeTypeDescription
classstringCSS class(es) to apply to the error message <div>

๐Ÿ› ๏ธ Methods

MethodParametersReturn TypeDescription
checkErrorsnonevoidChecks for errors on the bound control

๐Ÿ› ๏ธ ErrorMessageProviderService

A service that provides and manages global error messages used across the application.


Methods

MethodParametersReturn TypeDescription
registerMessagesmessages: ErrorMessagesvoidRegisters or overrides global error messages
getMessagesnoneErrorMessagesReturns all currently registered error messages
getMessageForErrorerrorKey: string, errorValue: anystringRetrieves the error message for a specific error key

๐Ÿšจ provideErrorHandling

A function that provides global error handling configuration for the entire Angular application.


๐Ÿ“ Parameters

NameTypeDefaultDescription
configErrorMessagesConfig{}Configuration object containing optional custom error messages
0.0.5

3 months ago

0.0.4

3 months ago

0.0.3

3 months ago

0.0.2

3 months ago

0.0.1

3 months ago