npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@house-of-angular/validation-messages

v18.0.0

Published

Package for handling validation messages in Angular

Downloads

186

Readme

Validation Messages

The Validation Messages package is designed to handle validation messages in Angular applications.

Installation

npm install @house-of-angular/validation-messages

Usage

1. Import ValidationMessagesModule

In the module where you want to use the ng-validation-messages component, import the ValidationMessagesModule as follows:

@NgModule({
  imports: [CommonModule, ValidationMessagesModule],
})
export class AppModule {}

2. Configuration

To configure the validation messages, you need to perform the following steps:

Step 2.1: Inject ValidationMessagesService

Inject the ValidationMessagesService into the constructor of the root module of your application or create a service that is provided at the root level and inject the ValidationMessagesService into it.

@NgModule({
  imports: [CommonModule, ValidationMessagesModule],
})
export class AppModule {
  constructor(private validationMessagesService: ValidationMessagesService) {}
}

or

@Injectable({
  providedIn: 'root',
})
export class ValidationMessagesConfigService {
  constructor(private validationMessagesService: ValidationMessagesService) {}
}

Step 2.2: Define Validation Messages

Define the global messages for validators by creating a configuration object of type ValidationMessagesConfig. There are two ways to provide error message definitions:

  • Using key-value pairs, where the key is the validator key in the FormControl's "errors" object and the value is the error message to be displayed. For example, see the required and email messages in the example configuration below.
  • Providing an object of type ValidationMessage under a key. For example, see the lettersPattern message in the example configuration below.

Here is an example configuration:

const validationMessagesConfig: ValidationMessagesConfig = {
  required: 'Field is required',
  email: 'Invalid email',
  min: 'Value is too small: {{actual}}/{{min}}',
  max: 'Value is too large {{actual}}/{{max}}',
  minlength: 'Too short {{actualLength}}/{{requiredLength}}',
  maxlength: 'Too long {{actualLength}}/{{requiredLength}}',
  customValidator: 'Custom validator error message',
  lettersPattern: {
    message: 'Must contains only letters',
    pattern: '$[a-zA-Z]*$',
  },
};

Once the configuration object is created, pass it as a parameter to the setValidationMessages method of the ValidationMessagesService.

const validationMessagesConfig: ValidationMessagesConfig = {
  required: 'Field is required',
  ...
};

@NgModule({
  imports: [...]
})
export class AppModule {
  constructor(private validationMessagesService: ValidationMessagesService) {
    this.validationMessagesService.setValidationMessages(validationMessagesConfig);
  }
}

3. Usage in templates

Now, in your component's template, you can use the ValidationMessagesComponent to display the validation messages. If you are using Angular Material, the validation messages component will automatically retrieve the form control from the mat-form-field.

<mat-form-field>
  <mat-label>Name</mat-label>
  <input matInput formControlName="name" />
  <mat-error>
    <ng-validation-messages></ng-validation-messages>
  </mat-error>
</mat-form-field>

Alternatively, you can manually pass the control to the ValidationMessagesComponent by specifying either the controlName or control input:

<ng-validation-messages [control]="name"></ng-validation-messages> <ng-validation-messages controlName="formControlName"></ng-validation-messages>

Local validator messages

You can extend and override the global validator messages by specifying local validator messages using the errorsMessages input:

<ng-validation-messages
  [errorsMessages]="{
      minlength: 'Required characters: {{actualLength}}/{{requiredLength}}',
    }"
></ng-validation-messages>

Interpolable parameters

You can specify parameters in the error messages using {{parameterName}}. These parameters allow you to access values from the error object. If the desired parameter is nested, you can use dot notation to access it, for example, {{rootKey.childKey}}.

4. API

ValidationMessagesService

Methods:

setValidationMessages(config: ValidationMessagesConfig): Sets validation messages configuration

setTemplateMatcher(matcher: RegExp): Sets specifies which part of the message string will be replaced with default interpolated value (the default matcher is /{{(.*)}}+/g)

useMaterialErrorMatcher(): If ValidationMessagesComponent is used together with custom errorStateMatcher for Angular Material's matInput and this method is called, the errors will be shown instantly and not on lost focus (errorStateMatcher needs to reflect that).

ValidationMessagesComponent

Inputs:

errorsMessages: ValidationMessagesConfig: Defines the local validation messages configuration. This configuration extends the global configuration.

multiple: boolean: Specifies whether to show multiple error messages (default: false)

apiErrorMessages: Array<string | ApiErrorMessage>: If an error is returned from an API request, this input allows to display it instantly

control: AbstractControl: Specifies the control for which errors should be shown.

controlName: string: Specifies the name of the form control for which errors should be shown.

Validation Message Interface

export interface ValidationMessage {
  message: string;
  validatorValue?: string;
  pattern?: string;
  validatorValueParser?: (value: any) => string;
  templateMatcher?: RegExp;
}

validatorValue: specifies the name of the property under the validator name in Form Control errors object from where the value for interpolation will be taken.

validatorValueParser: specifies a function to parse the validator value

templateMatcher: specifies which part of the message string will be replaced with interpolated value (the default matcher is /{{(.*)}}+/g)

pattern: specifies pattern for which the message will be shown when using Angular pattern validator