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

ng-form-validation-message

v1.0.2

Published

This package simplifies the way to display validation error messages on forms.

Downloads

17

Readme

Form Validation Messages

This package simplifies the way to display validation error messages on forms.

npm install ng-form-validation-message --save

Features

You can work with directives or components, display the first or all messages at the same time or/and customize error messages globally.

Usage

  1. Import the NgFormValidationMessageModule interface in order to use the directives and/or components:
import { NgModule } from '@angular/core';
import { NgFormValidationMessageModule } from 'ng-form-validation-message';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    ReactiveFormsModule,
    NgFormValidationMessageModule
  ],
})
export class AppModule { }
  1. Define your formControl in your component:
import { Component } from '@angular/core';
import { FormControl, Validators } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  myControl: FormControl = new FormControl(
    '',
    [Validators.required, Validators.email, Validators.minLength(5), Validators.maxLength(10)]
  );

}
  1. Use the component ng-form-validation-message, which you can use to display the first or all error messages by means of the [first] entry (true default), you can also send a [label] to interact with error messages:
<input [formControl]="myControl">
<ng-form-validation-message [control]="myControl" [first]="false" label="Username"></ng-form-validation-message>

<!--
  output with first:
  
  Username cannot be empty.

  or:

  Username is not a valid email address.
-->

<!--
  output without first:

  Username cannot be empty.
  Username is not a valid email address.
  Username must not be less than 5.
-->

or use directives to show one ngFormValidationMessage or all *ngFormValidationMessages

<input [formControl]="myControl">
<p ngFormValidationMessage [control]="myControl" label="Username"></p>

<!--
  output:
  
  Username cannot be empty.

  or:

  Username is not a valid email address.
-->
<input [formControl]="myControl">
<p *ngFormValidationMessages="let error; control: myControl; label: 'Username'">{{error}}</p>

<!--
  output:

  Username cannot be empty.
  Username is not a valid email address.
  Username must not be less than 5.
-->

Note that error messages will be displayed after the formControl is marked as touched

Example with Angular Material Design:

<mat-form-field>
  <mat-label>Username</mat-label>
  <input matInput [control]="myControl">
  <mat-error ngFormValidationMessage [control]="myControl" label="Username"></mat-error>
  <!-- <mat-error *ngFormValidationMessages="let error; control: myControl; label: 'Username'">{{error}}</mat-error> -->
</mat-form-field>

Example with Bootstrap 4.x:

<div class="form-group">
  <label for="...">Username</label>
  <input class="form-control" id="..." [control]="myControl">
  <div class="valid-feedback" ngFormValidationMessage [control]="myControl" label="Username"></div>
  <!-- <div class="valid-feedback" *ngFormValidationMessages="let error; control: myControl; label: 'Username'">{{error}}</div> -->
</div>

Example with formGroup and Angular Material Design:

  1. We create the form:
import { Component, OnInit } from '@angular/core';
import { Validators, FormGroup, FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

  myForm: FormGroup;

  constructor(
    private fb: FormBuilder
  ) {}

  ngOnInit() {
    this.createForm();
  }

  createForm() {
    this.myForm = this.fb.group({
      username: ['', [Validators.required, Validators.email, Validators.minLength(5), Validators.maxLength(10)]],
      password: ['', [Validators.required, Validators.minLength(8)]],
    });
  }

}
  1. Just like the example with formControl you can work with the single or multiple message directive. For this example, such directives are useful since we will use the design of the interior of your application. The difference is that in this case we must use the controlName property instead of [control]:
<form [formGroup]="myForm" (submit)="submit()">
  <mat-form-field>
    <mat-label>Username</mat-label>
    <input matInput formControlName="username">
    <mat-error ngFormValidationMessage controlName="username" label="Username"></mat-error>
  </mat-form-field>

  <mat-form-field>
    <mat-label>Password</mat-label>
    <input matInput formControlName="password" type="password">
    <mat-error ngFormValidationMessage controlName="password" label="Password"></mat-error>
  </mat-form-field>

  <button mat-raised-button color="primary" type="submit">SUBMIT</button>
</form>

Customization

Soon it will work with internationalization.

You can work with a collection of different messages:

import { NgModule } from '@angular/core';
import { NgFormValidationMessageModule, ERROR_MESSAGES } from 'ng-form-validation-message';

import { ReactiveFormsModule } from '@angular/forms';

export const customErrorMessages = {
  min: {
    message: '{label} must not be less than {min}.',
    rExp: 'min|actual'
  },
  max: {
    message: '{label} must not be greater than {max}.',
    rExp: 'max|actual'
  },
  required: {
    message: '{label} cannot be empty.'
  },
  requiredTrue: {
    message: '{label} must be true.'
  },
  email: {
    message: '{label} is not a valid email address.'
  },
  minlength: {
    message: '{label} should contain at least {requiredLength} character(s).',
    rExp: 'requiredLength|actualLength'
  },
  maxlength: {
    message: '{label} should contain at most {requiredLength} character(s).',
    rExp: 'requiredLength|actualLength'
  },
  pattern: {
    message: '{label} has an invalid format.',
    rExp: 'requiredPattern|actualValue'
  }
};

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    ReactiveFormsModule,
    NgFormValidationMessageModule
  ],
  providers: [
    {
      provide: ERROR_MESSAGES,
      useValue: customErrorMessages
    }
  ],
})
export class AppModule { }

Attention: consider the format of each value:

  • As you can see there is a message property that has the content of the message and the values to be personalized enclosed in braces

  • In the regEx property you have the aforementioned values separated by |.

You can work with a different label ('Field' by default):

import { NgModule } from '@angular/core';
import { NgFormValidationMessageModule, DEFAULT_LABEL } from 'ng-form-validation-message';

import { ReactiveFormsModule } from '@angular/forms';

@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ...
    ReactiveFormsModule,
    NgFormValidationMessageModule
  ],
  providers: [
    ...,
    {
      provide: DEFAULT_LABEL,
      useValue: 'MyLabel'
    }
  ],
})
export class AppModule { }