GithubHelp home page GithubHelp logo

mat-error-tailor's Introduction

Installation

npm install mat-error-tailor --save

Usage

import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { MatErrorTailorModule } from 'mat-error-tailor';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [
    FormsModule,
    ReactiveFormsModule,
    MatErrorTailorModule.forRoot({
      defaultErrors: {
        required: 'This field is required!',
        min: (error) => `The value cannot be lower than ${error.min}! You entered: ${error.actual}`,
        max: (error) => `The value cannot be heigher than ${error.max}! You entered: ${error.actual}`,
      },
      groupErrors: [
        {
          selector: 'formGroup',
          controls: [
            {
              selector: ['firstName', 'lastName'],
              errors: {
                pattern: 'Only characters are allowed!',
              },
            },
          ],
        },
      ],
      controlErrors: [
        {
          selector: 'singleControl',
          errors: {
            required: 'Custom required message',
          },
        },
      ],
    }),
  ],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}
<h2>Default Form Errors</h2>
<mat-divider></mat-divider>
<form class="form-group" [formGroup]="defaultForm" matErrorGroupName="defaultForm" autocomplete="off">
  <mat-form-field>
    <mat-label>Validators.required</mat-label>
    <input formControlName="required" matInput>
    <mat-error matErrorTailor></mat-error>
  </mat-form-field>
  <mat-form-field>
    <mat-label>Validators.min(10)</mat-label>
    <input formControlName="min" matInput>
    <mat-error matErrorTailor></mat-error>
  </mat-form-field>
  <mat-form-field>
    <mat-label>Validators.max(20)</mat-label>
    <input formControlName="max" matInput>
    <mat-error matErrorTailor></mat-error>
  </mat-form-field>
  <mat-form-field>
    <mat-label>Validators.minLength(5)</mat-label>
    <input formControlName="minLength" matInput>
    <mat-error matErrorTailor></mat-error>
  </mat-form-field>
  <mat-form-field>
    <mat-label>Validators.maxLength(5)</mat-label>
    <input formControlName="maxLength" matInput>
    <mat-error matErrorTailor matErrorControlName="dAD"></mat-error>
  </mat-form-field>
  <mat-form-field>
    <mat-label>Validators.pattern('^[0-9]*$')</mat-label>
    <input formControlName="pattern" matInput>
    <mat-error matErrorTailor></mat-error>
  </mat-form-field>
</form>

<h2>Form Group Errors</h2>
<form class="form-group" [formGroup]="formGroup" matErrorGroupName="formGroup" autocomplete="off">
  <mat-form-field>
    <mat-label>Validators.required, Validators.pattern('[a-zA-Z ]*')</mat-label>
    <input formControlName="firstName" matInput>
    <mat-error matErrorTailor></mat-error>
  </mat-form-field>
  <mat-form-field>
    <mat-label>Validators.required, Validators.pattern('[a-zA-Z ]*')</mat-label>
    <input formControlName="lastName" matInput>
    <mat-error matErrorTailor></mat-error>
  </mat-form-field>
</form>

<h2>Single Control</h2>
<div class="form-group">
  <mat-form-field>
    <mat-label>Single Control</mat-label>
    <input [formControl]="singleControl" matInput>
    <mat-error matErrorTailor matErrorControlName="singleControddl"></mat-error>
  </mat-form-field>
</div>
export class AppComponent {
  defaultForm: FormGroup;
  formGroup: FormGroup;
  singleControl: FormControl;

  constructor(private formBuilder: FormBuilder) {
    this.defaultForm = this.formBuilder.group({
      required: ['', Validators.required],
      min: ['', Validators.min(10)],
      max: ['', Validators.max(20)],
      minLength: ['', Validators.minLength(5)],
      maxLength: ['', Validators.maxLength(5)],
      pattern: ['', Validators.pattern('^[0-9]*$')],
    });

    this.formGroup = this.formBuilder.group({
      firstName: ['', [Validators.required, Validators.pattern('[a-zA-Z ]*')]],
      lastName: ['', Validators.required, Validators.pattern('[a-zA-Z ]*')],
    });

    this.singleControl = this.formBuilder.control('', [Validators.required]);
  }
}

mat-error-tailor's People

Contributors

dtomschitz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.