Skip to content

Instantly share code, notes, and snippets.

@third774
Created April 13, 2017 05:30
Show Gist options
  • Save third774/c805a96fb3e2ee03952c6873f08a52fa to your computer and use it in GitHub Desktop.
Save third774/c805a96fb3e2ee03952c6873f08a52fa to your computer and use it in GitHub Desktop.
Implementing global Custom Errors in ng-bootstrap-form-validation
import {BrowserModule} from "@angular/platform-browser";
import {NgModule} from "@angular/core";
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
import {HttpModule} from "@angular/http";
import {NgBootstrapFormValidationModule} from "ng-bootstrap-form-validation";
import {AppComponent} from "./app.component";
import {CUSTOM_ERRORS} from "./custom-errors";
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
NgBootstrapFormValidationModule.forRoot(CUSTOM_ERRORS),
HttpModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
import {ErrorMessage} from "ng-bootstrap-form-validation";
export const CUSTOM_ERRORS: ErrorMessage[] = [
{
error: "required",
format: requiredFormat
}, {
error: "email",
format: emailFormat
}
];
export function requiredFormat(label: string, error: any): string {
return `${label} IS MOST DEFINITELY REQUIRED!`;
}
export function emailFormat(label: string, error: any): string {
return `${label} doesn't look like a valid email address.`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment