ngx-mat-form-errors 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +175 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,24 +1,186 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ngx-mat-form-errors
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lightweight Angular library that automatically displays validation errors for Angular Material forms. Stop writing repetitive error message logic!
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/ngx-mat-form-errors)
|
|
6
|
+
[](https://www.npmjs.com/package/ngx-mat-form-errors)
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
> Note: Don't forget to add `--project ngx-form-errors` or else it will be added to the default project in your `angular.json` file.
|
|
8
|
+
## Installation
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
```bash
|
|
11
|
+
npm install ngx-mat-form-errors
|
|
12
|
+
```
|
|
11
13
|
|
|
12
|
-
|
|
14
|
+
## Quick Usage
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
### 1. Import the module
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
**For standalone components:**
|
|
19
|
+
```typescript
|
|
20
|
+
import { NgxFormErrorsModule } from 'ngx-mat-form-errors';
|
|
17
21
|
|
|
18
|
-
|
|
22
|
+
@Component({
|
|
23
|
+
// ...
|
|
24
|
+
imports: [
|
|
25
|
+
// ... other imports
|
|
26
|
+
NgxFormErrorsModule,
|
|
27
|
+
ReactiveFormsModule,
|
|
28
|
+
MatFormFieldModule,
|
|
29
|
+
MatInputModule
|
|
30
|
+
]
|
|
31
|
+
})
|
|
32
|
+
export class YourComponent {}
|
|
33
|
+
```
|
|
19
34
|
|
|
20
|
-
|
|
35
|
+
**For NgModules:**
|
|
36
|
+
```typescript
|
|
37
|
+
import { NgxFormErrorsModule } from 'ngx-mat-form-errors';
|
|
38
|
+
|
|
39
|
+
@NgModule({
|
|
40
|
+
imports: [
|
|
41
|
+
// ... other imports
|
|
42
|
+
NgxFormErrorsModule,
|
|
43
|
+
ReactiveFormsModule,
|
|
44
|
+
MatFormFieldModule,
|
|
45
|
+
MatInputModule
|
|
46
|
+
]
|
|
47
|
+
})
|
|
48
|
+
export class AppModule {}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 2. Create a form
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
55
|
+
|
|
56
|
+
export class YourComponent {
|
|
57
|
+
form: FormGroup;
|
|
58
|
+
|
|
59
|
+
constructor(private fb: FormBuilder) {
|
|
60
|
+
this.form = this.fb.group({
|
|
61
|
+
email: ['', [Validators.required, Validators.email]],
|
|
62
|
+
password: ['', [Validators.required, Validators.minLength(8)]]
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 3. Add to template
|
|
69
|
+
|
|
70
|
+
```html
|
|
71
|
+
<form [formGroup]="form">
|
|
72
|
+
<!-- Email field -->
|
|
73
|
+
<mat-form-field>
|
|
74
|
+
<mat-label>Email</mat-label>
|
|
75
|
+
<input matInput formControlName="email">
|
|
76
|
+
</mat-form-field>
|
|
77
|
+
<ngx-error field="email" label="Email"></ngx-error>
|
|
78
|
+
|
|
79
|
+
<!-- Password field -->
|
|
80
|
+
<mat-form-field>
|
|
81
|
+
<mat-label>Password</mat-label>
|
|
82
|
+
<input matInput type="password" formControlName="password">
|
|
83
|
+
</mat-form-field>
|
|
84
|
+
<ngx-error field="password" label="Password"></ngx-error>
|
|
85
|
+
</form>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
That's it! Errors will automatically show when fields are invalid and touched.
|
|
89
|
+
|
|
90
|
+
## Examples
|
|
91
|
+
|
|
92
|
+
### Basic Form
|
|
93
|
+
|
|
94
|
+
```html
|
|
95
|
+
<mat-form-field>
|
|
96
|
+
<mat-label>Username</mat-label>
|
|
97
|
+
<input matInput formControlName="username">
|
|
98
|
+
</mat-form-field>
|
|
99
|
+
<ngx-error field="username" label="Username"></ngx-error>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Custom Label
|
|
103
|
+
|
|
104
|
+
```html
|
|
105
|
+
<ngx-error field="email" label="Email address"></ngx-error>
|
|
106
|
+
<!-- Displays: "Email address is required" instead of "Email is required" -->
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Nested Form Groups
|
|
110
|
+
|
|
111
|
+
```html
|
|
112
|
+
<div formGroupName="address">
|
|
113
|
+
<mat-form-field>
|
|
114
|
+
<mat-label>Street</mat-label>
|
|
115
|
+
<input matInput formControlName="street">
|
|
116
|
+
</mat-form-field>
|
|
117
|
+
<ngx-error field="address.street" label="Street"></ngx-error>
|
|
118
|
+
</div>
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Default Error Messages
|
|
122
|
+
|
|
123
|
+
| Error | Message |
|
|
124
|
+
|-------|---------|
|
|
125
|
+
| required | `{label} is required` |
|
|
126
|
+
| email | `Invalid email format` |
|
|
127
|
+
| minlength | `{label} must be at least {requiredLength} characters` |
|
|
128
|
+
| maxlength | `{label} cannot exceed {requiredLength} characters` |
|
|
129
|
+
| pattern | `Invalid {label} format` |
|
|
130
|
+
| default | `Invalid {label}` |
|
|
131
|
+
|
|
132
|
+
## Custom Error Messages
|
|
133
|
+
|
|
134
|
+
Create a custom service:
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { Injectable } from '@angular/core';
|
|
138
|
+
import { ErrorService } from 'ngx-mat-form-errors';
|
|
139
|
+
|
|
140
|
+
@Injectable({ providedIn: 'root' })
|
|
141
|
+
export class CustomErrorService extends ErrorService {
|
|
142
|
+
override getErrorMessage(errors: any, label: string): string {
|
|
143
|
+
if (errors['required']) return `Please enter your ${label}`;
|
|
144
|
+
if (errors['email']) return 'Enter a valid email address';
|
|
145
|
+
if (errors['minlength']) return `Minimum ${errors['minlength'].requiredLength} characters required`;
|
|
146
|
+
return super.getErrorMessage(errors, label);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Add to providers:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
providers: [
|
|
155
|
+
{ provide: ErrorService, useClass: CustomErrorService }
|
|
156
|
+
]
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## API
|
|
160
|
+
|
|
161
|
+
### `<ngx-error>` Component
|
|
162
|
+
|
|
163
|
+
| Property | Type | Required | Description |
|
|
164
|
+
|----------|------|----------|-------------|
|
|
165
|
+
| `field` | string | Yes | Form control name (supports nested like "address.street") |
|
|
166
|
+
| `label` | string | No | Display name (defaults to field name) |
|
|
167
|
+
|
|
168
|
+
## Styling
|
|
169
|
+
|
|
170
|
+
The component uses the `.ngx-error` class:
|
|
171
|
+
|
|
172
|
+
```css
|
|
173
|
+
.ngx-error {
|
|
174
|
+
color: #f44336;
|
|
175
|
+
font-size: 12px;
|
|
176
|
+
margin-top: 4px;
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Requirements
|
|
181
|
+
|
|
182
|
+
- Angular 14+
|
|
183
|
+
- @angular/forms
|
|
184
|
+
- @angular/material (optional)
|
|
21
185
|
|
|
22
|
-
## Further help
|
|
23
186
|
|
|
24
|
-
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.dev/tools/cli) page.
|