Skip to content
This repository was archived by the owner on Jan 5, 2022. It is now read-only.

Commit fd638b3

Browse files
committed
Documentation improvements
1 parent 0de52f1 commit fd638b3

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

README.md

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ export class AppModule {
3737
}
3838
```
3939

40-
## Usage
40+
## Basics
4141

42-
### Basics
42+
### Default Errors
43+
44+
By default, the validators found on the `Validators` class from `@angular/forms` module are handled for you out of the box. All you need to do is import the module.
45+
46+
### Usage
4347
ng-bootstrap-form-validation works by using the `form-group` Bootstrap class on your divs as component selector, and projecting the content into a component which handles form validation feedback for you.
4448

4549
The `has-error` and `has-success` classes are automatically added or removed to your `form-group` based on whether or not the input is valid, and is both `touched` and `dirty`.
@@ -110,32 +114,69 @@ export class BasicExampleComponent implements OnInit {
110114
</div>
111115
```
112116

113-
### Custom Messages
117+
## Custom Error Messages
114118

115-
Optionally, you can pass an `ErrorMessage` array into the `.forRoot()` method in your `app.module.ts` to provide custom errors across your entire app. The `ErrorMessage` interface looks like this:
119+
### Global Custom Errors
116120

121+
Optionally, you can pass an `ErrorMessage` array into the `.forRoot()` method in your `app.module.ts` to provide custom errors across your entire app. In order for this to be AOT compatable, the function definitions **must** be exported. see below for an example
122+
123+
`custom-errors.ts`
117124
```ts
118-
/**
119-
* Interface for creating validation messages
120-
*/
121-
export interface ErrorMessage {
122-
/**
123-
* The error key to look for on the FormControl.errors object
124-
*/
125-
error: string;
126-
/**
127-
* The message string function to create the validation message to be displayed.
128-
* @param {string} label The text from the first <label> tag found within the .form-group
129-
* @param {*} error The value accessed from FormControl.errors[error] using ErrorMessage.error as the key
130-
*/
131-
format?: (label?: string, error?: any) => string;
125+
import {ErrorMessage} from "ng-bootstrap-form-validation";
126+
127+
export const CUSTOM_ERRORS: ErrorMessage[] = [
128+
{
129+
error: "required",
130+
format: requiredFormat
131+
}, {
132+
error: "email",
133+
format: emailFormat
134+
}
135+
];
136+
137+
export function requiredFormat(label: string, error: any): string {
138+
return `${label} IS MOST DEFINITELY REQUIRED!`;
139+
}
140+
141+
export function emailFormat(label: string, error: any): string {
142+
return `${label} doesn't look like a valid email address.`;
143+
}
144+
```
145+
146+
`app.module.ts`
147+
```ts
148+
import {BrowserModule} from "@angular/platform-browser";
149+
import {NgModule} from "@angular/core";
150+
import {FormsModule, ReactiveFormsModule} from "@angular/forms";
151+
import {HttpModule} from "@angular/http";
152+
import {NgBootstrapFormValidationModule} from "ng-bootstrap-form-validation";
153+
import {AppComponent} from "./app.component";
154+
import {CUSTOM_ERRORS} from "./custom-errors";
155+
156+
@NgModule({
157+
declarations: [
158+
AppComponent
159+
],
160+
imports: [
161+
BrowserModule,
162+
FormsModule,
163+
ReactiveFormsModule,
164+
NgBootstrapFormValidationModule.forRoot(CUSTOM_ERRORS),
165+
HttpModule
166+
],
167+
providers: [],
168+
bootstrap: [AppComponent]
169+
})
170+
export class AppModule {
132171
}
133172
```
134173

174+
### Form Control Specific Custom Errors
175+
135176
In addition to providing custom errors at the top level using the `.forRoot()` method,
136177
you can provide custom error messages to a specific control by binding to the
137178
`customErrorMessages` directive on the `.form-group` element. Modifying the basic
138-
example above, we can provide a one time custom error message to a specific `.form-group`
179+
example above, we can provide a one time custom error message to a specific `.form-group`. Unline the global custom error messages, these do not need to be individually exported.
139180

140181
`custom-error-example.component.ts`
141182
```ts

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-bootstrap-form-validation",
3-
"version": "0.0.7",
3+
"version": "0.0.8",
44
"description": "README, an Angular library",
55
"main": "dist/ng-bootstrap-form-validation.bundle.js",
66
"scripts": {

0 commit comments

Comments
 (0)