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
{{ message }}
This repository was archived by the owner on May 3, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+87-15Lines changed: 87 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,12 +28,26 @@ The design of this library promotes less boilerplate code, which keeps your temp
28
28
29
29
## Table of Contents
30
30
31
+
-[How it works](#how_it_works)
31
32
-[Installation](#installation)
32
33
-[Usage](#usage)
33
34
-[Advanced configuration](#configuration)
35
+
-[Handling form submission](#handling_form_submission)
36
+
-[Getting error details](#getting_error_details)
34
37
-[Styling](#styling)
35
38
-[Development](#development)
36
39
40
+
## How it works
41
+
42
+
There are a few rules that the library follows to determine when to display errors:
43
+
44
+
- Errors will be shown no matter what configuration you're using after form is submitted.
45
+
- If no configuration is provided, the errors will be shown when control is `touched`.
46
+
- This is chosen due to the UX guidelines. Read (1) [How to Report Errors in Forms](https://www.nngroup.com/articles/errors-forms-design-guidelines/) and (2) [Designing More Efficient Forms](https://uxplanet.org/designing-more-efficient-forms-assistance-and-validation-f26a5241199d) for more info.
47
+
- If you configured errors to be shown when `formIsSubmitted`, but dealing with a control that does not have a parent _form_, the config for this control will fall back to `touched`.
48
+
49
+
For more info about this see [Advanced configuration](#configuration).
50
+
37
51
## Installation
38
52
39
53
### NPM
@@ -119,9 +133,9 @@ export class MyAppModule {}
119
133
Alternatively, use dependency injection to provide configuration at a component level:
@@ -135,26 +149,83 @@ export class MyComponent { }
135
149
Here's the configuration object interface:
136
150
137
151
```ts
138
-
interfaceIErrorsConfiguration {
152
+
exportinterfaceIErrorsConfiguration {
139
153
/**
140
-
* Configure errors to show only when the corresponding input is dirty.
154
+
* Configures when to display an error for an invalid control. Available options are:
141
155
*
142
-
* Default is `true`.
143
-
*/
144
-
showErrorsOnlyIfInputDirty?:boolean;
145
-
146
-
/**
147
-
* Configure errors to show only when form is submitted.
148
-
* Upon form submission shows errors even if `showErrorsOnlyIfInputDirty = true`
149
-
* and some of the inputs aren't dirty.
150
-
* Takes effect only when ngxErrors directive is a child of a form.
156
+
* `'touched'` - *[default]* shows an error when control is marked as touched. For example, user focused on the input and clicked away or tabbed through the input.
157
+
*
158
+
* `'dirty'` - shows an error when control is marked as dirty. For example, when user has typed something in.
151
159
*
152
-
* Default is `false`.
160
+
* `'touchedAndDirty'` - shows an error when control is marked as both - touched and dirty.
161
+
*
162
+
* `'formIsSubmitted'` - shows an error when parent form was submitted.
153
163
*/
154
-
showErrorsWhenFormSubmitted?:boolean;
164
+
showErrorsWhenInput:ShowErrorWhen;
155
165
}
166
+
167
+
exporttypeShowErrorWhen=
168
+
|'touched'
169
+
|'dirty'
170
+
|'touchedAndDirty'
171
+
|'formIsSubmitted';
172
+
```
173
+
174
+
### Overriding global config
175
+
176
+
You can override the configuration specified at the module level by using `[showWhen]` input on `[ngxErrors]` and on `[ngxError]` directives:
This will be shown when control is touched and dirty
186
+
</div>
187
+
</div>
156
188
```
157
189
190
+
## Handling form submission
191
+
192
+
Often there's a requirement to submit a form when user presses _Enter_. Under the hood ngxError relies on form submit event to display errors. That is why it's important to trigger form submission properly rather than binding `(keyup.enter)` event to the method in your component class directly. Here's how to do that:
193
+
194
+
```html
195
+
<form
196
+
[formGroup]="form"
197
+
(ngSubmit)="yourMethod()"
198
+
(keyup.enter)="submitBtn.click()"
199
+
>
200
+
...
201
+
202
+
<button#submitBtn>Submit</button>
203
+
</form>
204
+
```
205
+
206
+
## Getting error details
207
+
208
+
Each control error in Angular may contain additional details. For example, here's what `min` error looks like:
209
+
210
+
```ts
211
+
const control =newFormControl(3, Validators.min(10));
You can easily get access to these details in the template:
217
+
218
+
```html
219
+
<divngxErrors="control">
220
+
<divngxError="min"#myMin="ngxError">
221
+
Number should be greater than {{myMin.err.min}}. You've typed
222
+
{{myMin.err.actual}}.
223
+
</div>
224
+
</div>
225
+
```
226
+
227
+
In the example above we're assigning a variable `myMin` (can be anything you want) to the directive `ngxError`. Using this variable we can access the context of the directive. The directive has property `err` that contains all the error details.
228
+
158
229
## Styling
159
230
160
231
Include something similar to the following in global CSS file:
@@ -207,6 +278,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
207
278
208
279
<!-- markdownlint-enable -->
209
280
<!-- prettier-ignore-end -->
281
+
210
282
<!-- ALL-CONTRIBUTORS-LIST:END -->
211
283
212
284
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
0 commit comments