Skip to content

Commit

Permalink
docs: ✏️ clarify config errors usage
Browse files Browse the repository at this point in the history
Now you can pass to the errors property in config an `HashMap<string |
(err:any)=>string>` so you can specify the error message directly as a
simple string, or with a function that will return the message string
based on the error.

BREAKING CHANGE: No
  • Loading branch information
dmorosinotto committed Jun 12, 2020
1 parent 998f32d commit a2a6f29
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
20 changes: 20 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@
"code",
"test"
]
},
{
"login": "theblushingcrow",
"name": "Inbal Sinai",
"avatar_url": "https://avatars3.githubusercontent.com/u/638818?v=4",
"profile": "https://github.com/theblushingcrow",
"contributions": [
"doc"
]
},
{
"login": "dmorosinotto",
"name": "Daniele Morosinotto",
"avatar_url": "https://avatars2.githubusercontent.com/u/3982050?v=4",
"profile": "https://twitter.com/dmorosinotto",
"contributions": [
"code",
"doc",
"example"
]
}
],
"contributorsPerLine": 7
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Sit back, relax, and let the Error Tailor do all the work!

Run `ng add @ngneat/error-tailor`. This command updates the `AppModule`, and adds the `ErrorTailorModule` dependency:

<!-- prettier-ignore-start -->
```ts
@NgModule({
declarations: [AppComponent],
Expand All @@ -32,8 +33,9 @@ Run `ng add @ngneat/error-tailor`. This command updates the `AppModule`, and add
ErrorTailorModule.forRoot({
errors: {
useValue: {
required: error => `This field is required`,
minlength: ({ requiredLength, actualLength }) => `Expect ${requiredLength} but got ${actualLength}`,
required: 'This field is required',
minlength: ({ requiredLength, actualLength }) =>
`Expect ${requiredLength} but got ${actualLength}`,
invalidAddress: error => `Address isn't valid`
}
}
Expand All @@ -43,8 +45,9 @@ Run `ng add @ngneat/error-tailor`. This command updates the `AppModule`, and add
})
export class AppModule {}
```
<!-- prettier-ignore-end -->

The `errors` config property takes a partial `Provider`, that should provide an object containing the form errors.
The `errors` config property takes a partial `Provider`, that should provide a `HashMap< string | (err:any) => string >` that is an object with keys corresponding to the errors name that you want to handle, and values that can be a simple string, or function that return a string used as error message to be shown.
Now the only thing you need to add is the `errorTailor` directive to your form:

```html
Expand Down Expand Up @@ -232,13 +235,13 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<tr>
<td align="center"><a href="https://www.netbasal.com"><img src="https://avatars1.githubusercontent.com/u/6745730?v=4" width="100px;" alt=""/><br /><sub><b>Netanel Basal</b></sub></a><br /><a href="https://github.com/@ngneat/error-tailor/commits?author=NetanelBasal" title="Code">💻</a> <a href="https://github.com/@ngneat/error-tailor/commits?author=NetanelBasal" title="Documentation">📖</a> <a href="#ideas-NetanelBasal" title="Ideas, Planning, & Feedback">🤔</a> <a href="#infra-NetanelBasal" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a></td>
<td align="center"><a href="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/tonivj5"><img src="https://avatars2.githubusercontent.com/u/7110786?v=4" width="100px;" alt=""/><br /><sub><b>Toni Villena</b></sub></a><br /><a href="https://github.com/@ngneat/error-tailor/commits?author=tonivj5" title="Code">💻</a> <a href="https://github.com/@ngneat/error-tailor/commits?author=tonivj5" title="Tests">⚠️</a></td>
<td align="center"><a href="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/theblushingcrow"><img src="https://avatars3.githubusercontent.com/u/638818?v=4" width="100px;" alt=""/><br /><sub><b>Inbal Sinai</b></sub></a><br /><a href="https://github.com/@ngneat/error-tailor/commits?author=theblushingcrow" title="Documentation">📖</a> </td>
<td align="center"><a href="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/theblushingcrow"><img src="https://avatars3.githubusercontent.com/u/638818?v=4" width="100px;" alt=""/><br /><sub><b>Inbal Sinai</b></sub></a><br /><a href="https://github.com/@ngneat/error-tailor/commits?author=theblushingcrow" title="Documentation">📖</a></td>
<td align="center"><a href="https://twitter.com/dmorosinotto"><img src="https://avatars2.githubusercontent.com/u/3982050?v=4" width="100px;" alt=""/><br /><sub><b>Daniele Morosinotto</b></sub></a><br /><a href="https://github.com/@ngneat/error-tailor/commits?author=dmorosinotto" title="Code">💻</a> <a href="https://github.com/@ngneat/error-tailor/commits?author=dmorosinotto" title="Documentation">📖</a> <a href="#example-dmorosinotto" title="Examples">💡</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class ControlErrorsDirective implements OnInit, OnDestroy {
ngOnDestroy() {
this.destroy.next();
if (this.ref) this.ref.destroy();
this.ref = null;
}

private valueChanges() {
Expand Down

0 comments on commit a2a6f29

Please sign in to comment.