You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -42,10 +46,12 @@ Form validation error reporting made easy for reactive forms. Typically you'd do
42
46
</div>
43
47
```
44
48
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:
46
50
47
51
```js
52
+
<!--with ngxErrors -->
48
53
<input type="text" formControlName="foo">
54
+
49
55
<div ngxErrors="foo">
50
56
<div ngxError="required" when="touched">
51
57
Field is required
@@ -183,6 +189,90 @@ ngxErrors also supports FormGroups with control names using dot notation:
183
189
</div>
184
190
```
185
191
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`.
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
+
<inputtype="text"formControlName="username">
210
+
211
+
<divngxErrors="username"#myError="ngxErrors">
212
+
<divngxError="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.
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
+
<inputtype="text"formControlName="username">
247
+
</div>
248
+
249
+
<divngxErrors="username"#myError="ngxErrors">
250
+
<divngxError="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
+
<inputtype="text"formControlName="username">
264
+
265
+
<divngxErrors="username"#myError="ngxErrors">
266
+
<divngxError="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
0 commit comments