Skip to content

Commit b9c99f3

Browse files
committed
chore(readme): add new APIs
1 parent 817aafb commit b9c99f3

1 file changed

Lines changed: 94 additions & 4 deletions

File tree

README.md

Lines changed: 94 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<img width="40" valign="bottom" src="https://angular.io/resources/images/logos/angular/angular.svg">
88
ngxErrors
99
</h1>
10-
<h4 align="center">A declarative validation module for reactive forms.</h4>
10+
<h4 align="center">A declarative validation errors module for reactive forms.</h4>
1111

1212
---
1313

@@ -26,14 +26,18 @@ ngxErrors
2626

2727
# Overview
2828

29-
Why use ngxErrors, how to install and include.
29+
Why use ngxErrors, how to install and use.
3030

3131
### What is it?
3232

33-
Form validation error reporting made easy for reactive forms. Typically you'd do something like this:
33+
Declarative validation error messages for reactive forms.
34+
35+
Typically you'd do something like this:
3436

3537
```js
38+
<!-- without ngxErrors -->
3639
<input type="text" formControlName="foo">
40+
3741
<div *ngIf="form.get('foo').hasError('required') && form.get('foo').touched">
3842
Field is required
3943
</div>
@@ -42,10 +46,12 @@ Form validation error reporting made easy for reactive forms. Typically you'd do
4246
</div>
4347
```
4448

45-
With ngxErrors, we've taken a simple declarative approach that cleans up your templates:
49+
With ngxErrors, we've taken a simple declarative approach that cleans up your templates to make validation easier:
4650

4751
```js
52+
<!-- with ngxErrors -->
4853
<input type="text" formControlName="foo">
54+
4955
<div ngxErrors="foo">
5056
<div ngxError="required" when="touched">
5157
Field is required
@@ -183,6 +189,90 @@ ngxErrors also supports FormGroups with control names using dot notation:
183189
</div>
184190
```
185191

192+
### Exported Directive API
193+
194+
ngxErrors exports itself as `ngxErrors`, which means you can access information about your control error states elsewhere in your template using a template reference, such as `#foo`.
195+
196+
Basic usage:
197+
198+
```html
199+
<div ngxErrors="store.code" #code="ngxErrors"></div>
200+
```
201+
202+
#### getError(name: string): any;
203+
204+
The `getError` method returns the object associated with your error. This can be useful for dynamically displaying error rules.
205+
206+
> Example: Adds `Min length is 5` when value is less than 5 characters (based on `Validators.minLength(5)`).
207+
208+
```html
209+
<input type="text" formControlName="username">
210+
211+
<div ngxErrors="username" #myError="ngxErrors">
212+
<div ngxError="minlength" [when]="dirty">
213+
Min length should be {{ myError.getError('minlength')?.requiredLength }}
214+
</div>
215+
</div>
216+
```
217+
218+
> The error returned is identical to Angular's FormControl API
219+
220+
#### hasError(name: string): boolean;
221+
222+
The `hasError` method informs you if your control has the given error. This can be useful for styling elsewhere in your template based off the control's error state.
223+
224+
> Example: Adds `class="required"` when "myError" has the `required` error.
225+
226+
```html
227+
<div [ngClass]="{ required: myError.hasError('required') }">
228+
<input type="text" formControlName="username">
229+
</div>
230+
231+
<div ngxErrors="username" #myError="ngxErrors">
232+
<div ngxError="minlength" [when]="dirty">
233+
Min length is 5
234+
</div>
235+
</div>
236+
```
237+
238+
#### hasErrors: boolean;
239+
240+
The `hasErrors` property returns `true` if your control has any number of errors. This can be useful for styling elsewhere in your template on a global control level rather than individual errors.
241+
242+
> Example: Adds `class="required"` when "myError" has the `required` error.
243+
244+
```html
245+
<div [ngClass]="{ hasErrors: myError.hasErrors }">
246+
<input type="text" formControlName="username">
247+
</div>
248+
249+
<div ngxErrors="username" #myError="ngxErrors">
250+
<div ngxError="minlength" [when]="dirty">
251+
Min length is 5
252+
</div>
253+
</div>
254+
```
255+
256+
#### errors: { [key: string]: any; };
257+
258+
The `errors` property returns the object associated with any active errors. This can be used to access any error properties on your control.
259+
260+
> Example: Adds `Max length is 10, you typed (n)` when value is more than 10 characters (based on `Validators.maxLength(10)`).
261+
262+
```html
263+
<input type="text" formControlName="username">
264+
265+
<div ngxErrors="username" #myError="ngxErrors">
266+
<div ngxError="minlength" [when]="dirty">...</div>
267+
</div>
268+
269+
<div *ngIf="myError.errors?.maxlength">
270+
Max length is 10, you typed {{ myError.errors.maxlength.actualLength }}
271+
</div>
272+
```
273+
274+
> The errors returned are identical to Angular's FormControl API
275+
186276
[circle-badge]: https://circleci.com/gh/UltimateAngular/ngxerrors.svg?style=shield
187277
[circle-badge-url]: https://circleci.com/gh/UltimateAngular/ngxerrors
188278
[david-badge]: https://david-dm.org/UltimateAngular/ngxerrors.svg

0 commit comments

Comments
 (0)