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

ng2-mdf-validation-messages

v0.1.5

Published

Angular 2 model driven forms validation messages module.

Downloads

9

Readme

Angular 2 Model Driven Forms - Validation Messages

The idea behind ng2-mdf-validation-messages is to make your Angular 2 forms validation easier, faster and with less code in a way that it is like using the original Angular 2 validations. It currently supports default and custom error messages. Global and local configuration. Just one line of code to show errors.

Dependencies

No external dependencies except Angular 2 itself.

Angular 2 Version

This is currently written with version 2.1.2, but it should work with 2.0.0 even RC6.

Quick start

  1. A recommended way to install ng2-mdf-validation-messages is through npm package manager using the following command:

npm install ng2-mdf-validation-messages --save

Usage

Import Ng2MDFValidationMessagesModule into your app's modules:

import { Ng2MDFValidationMessagesModule } from 'ng2-mdf-validation-messages';

@NgModule({
  imports: [
    Ng2MDFValidationMessagesModule
  ]
})

This makes all the ng2-mdf-validation-messages components available for use in your app components.

Basic Example

You can also check the demo-app in the repository for more complete examples.

Component

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';

import { ValidationExtensions } from 'ng2-mdf-validation-messages';

@Component({
    selector: 'my-app',
    templateUrl: './src/app.component.html',
})
export class AppComponent implements OnInit {
    editorForm: FormGroup;
    firstName: FormControl;

    constructor(private formBuilder: FormBuilder) {}

    ngOnInit() {
        this.firstName = this.formBuilder.control('', ValidationExtensions.required());
        this.editorForm = this.formBuilder.group({
            firstName: this.firstName,
        });
    }
}

Template

<form [formGroup]="editorForm" novalidate>
    <label>First Name</label>
    <input formControlName="firstName" type="text">
    <ng2-mdf-validation-message [control]="firstName" *ngIf="!firstName.pristine"></ng2-mdf-validation-message>
</form>

Advanced (and actually useful) Examples

Global error messages configuration.

ng2-mdf-validation-messages comes with the option to configure globally the messages that the errors return and the class of the div where the error is displayed.

import { Ng2MDFValidationMessagesModule } from 'ng2-mdf-validation-messages';

@NgModule({
  imports: [
    Ng2MDFValidationMessagesModule.globalConfig({
        class: 'text-danger',
        defaultErrorMessages: {
            required: 'Default Custom Required Message',
            email: 'Invalid email!',
            minLength: 'Minimum length is {0}!',
        }
    })
  ]
})

As you can see placeholders in the strings are supported. For this example, ValidationExtensions.minLength(3) will output error 'Minimum length is 3!'

Currently supported errors and their default messages.

  • required: 'This field is required!',
  • pattern: 'The input value does not match the pattern required!',
  • email: 'Invalid email!',
  • minLength: 'Minimum length is {0}!',
  • maxLength: 'Maximum length is {0}!',
  • minNumber: 'Minimal value is {0}!',
  • maxNumber: 'Maximal value is {0}!',
  • noEmpty: 'Only blank spaces are not allowed!',
  • rangeLength: 'The input must be between {0} and {1} symbols long!',
  • range: 'The input must be between {0} and {1}!',
  • digit: 'The input must be a number!',
  • equal: 'The input must be equal to {0}!',
  • url: 'The input must be a valid URL!',
  • date: 'The input must be a valid date!',
  • areEqual: 'The input values in the group must match!',
  • passwords: 'Both fields "Password" and "Confirm Password" must match!',
  • unknownError: 'Unknown Error!',

Configure specific errors

The Style

You can configure the class of the div where the error is shown locally on each error.

<ng2-mdf-validation-message [control]="firstName" *ngIf="!firstName.pristine" [class]="'text-danger'"></ng2-mdf-validation-message>

Custom error messages

The real "power" of this component is the ability to give custom error messages for every single validation. And again placeholders are supported. Example:

    this.firstName = this.formBuilder.control('', ValidationExtensions.required('First name is required!'));

    this.username = this.formBuilder.control('', [
            ValidationExtensions.required('Username is required!'),
            ValidationExtensions.minLength(3, 'Username must be at least {0} symbols long!')
        ]);

    this.age = this.formBuilder.control('', ValidationExtensions.minNumber(18, 'Minimum age to enter is {0}!'));
}

Custom Validation Extensions

You can create your custom validations following this example:

static VALIDATION_NAME(...PROPS_IF_NEEDED, message: string): ValidatorFn {
    return (control: AbstractControl): { [key: string]: any } => {
        // VALIDATION LOGIC

       return {
            ERROR_TYPE: {
                message: message,
                //...PROPS (will be used for placeholders)
            }
        };
    };
}

TODOs

  • Add compose

Development

  1. Download this repo.
  2. Run npm install.
  3. Run npm start
  4. Go to demo-app
  5. Run npm install.
  6. Run npm start
  7. To build run npm run build.prod
  8. Run tests npm run test