@muziehdesign/forms 0.0.1 → 0.0.397-alpha
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/esm2020/lib/constants.mjs +2 -2
- package/esm2020/lib/field-error.mjs +2 -2
- package/esm2020/lib/field-errors/field-errors.component.mjs +18 -0
- package/esm2020/lib/forms.module.mjs +27 -14
- package/esm2020/lib/masks.mjs +62 -0
- package/esm2020/lib/model-schema.factory.mjs +126 -58
- package/esm2020/lib/model-state-options.mjs +2 -0
- package/esm2020/lib/model-state-result.mjs +2 -0
- package/esm2020/lib/model-validator.mjs +16 -16
- package/esm2020/lib/ng-form-model-state.service.mjs +22 -0
- package/esm2020/lib/ngform-model-state.mjs +105 -0
- package/esm2020/lib/type-annotations.mjs +81 -0
- package/esm2020/muziehdesign-forms.mjs +4 -4
- package/esm2020/public-api.mjs +14 -9
- package/fesm2015/muziehdesign-forms.mjs +448 -112
- package/fesm2015/muziehdesign-forms.mjs.map +1 -1
- package/fesm2020/muziehdesign-forms.mjs +440 -112
- package/fesm2020/muziehdesign-forms.mjs.map +1 -1
- package/index.d.ts +5 -5
- package/lib/constants.d.ts +1 -1
- package/lib/field-error.d.ts +5 -5
- package/lib/field-errors/field-errors.component.d.ts +8 -0
- package/lib/forms.module.d.ts +8 -6
- package/lib/masks.d.ts +42 -0
- package/lib/model-schema.factory.d.ts +14 -10
- package/lib/model-state-options.d.ts +4 -0
- package/lib/model-state-result.d.ts +6 -0
- package/lib/model-validator.d.ts +7 -7
- package/lib/ng-form-model-state.service.d.ts +12 -0
- package/lib/ngform-model-state.d.ts +25 -0
- package/lib/type-annotations.d.ts +116 -0
- package/package.json +7 -2
- package/public-api.d.ts +10 -5
- package/esm2020/lib/property-schema-metadata.mjs +0 -2
- package/esm2020/lib/property-type.enum.mjs +0 -8
- package/esm2020/lib/string-schema.mjs +0 -27
- package/lib/property-schema-metadata.d.ts +0 -6
- package/lib/property-type.enum.d.ts +0 -6
- package/lib/string-schema.d.ts +0 -15
|
@@ -1,130 +1,466 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { Injectable, NgModule } from '@angular/core';
|
|
3
|
-
import
|
|
2
|
+
import { Injectable, Component, Input, NgModule } from '@angular/core';
|
|
3
|
+
import * as Yup from 'yup';
|
|
4
|
+
import { object } from 'yup';
|
|
4
5
|
import 'reflect-metadata';
|
|
6
|
+
import * as i1 from '@angular/common';
|
|
7
|
+
import { CommonModule } from '@angular/common';
|
|
8
|
+
import { __awaiter } from 'tslib';
|
|
9
|
+
import { FormGroup, FormArray } from '@angular/forms';
|
|
10
|
+
import { BehaviorSubject, switchMap, from } from 'rxjs';
|
|
11
|
+
import { format, parse, isValid } from 'date-fns';
|
|
12
|
+
import * as IMask from 'imask';
|
|
5
13
|
|
|
6
|
-
class ModelValidator {
|
|
7
|
-
constructor(modelSchema) {
|
|
8
|
-
this.schema = modelSchema;
|
|
9
|
-
}
|
|
10
|
-
validate(model) {
|
|
11
|
-
return this.schema
|
|
12
|
-
.validate(model, { abortEarly: false })
|
|
13
|
-
.then(() => {
|
|
14
|
-
return [];
|
|
15
|
-
})
|
|
16
|
-
.catch((e) => {
|
|
17
|
-
return e.inner.map((error) => ({ path: error.path, type: error.type, message: error.message }));
|
|
18
|
-
});
|
|
19
|
-
}
|
|
14
|
+
class ModelValidator {
|
|
15
|
+
constructor(modelSchema) {
|
|
16
|
+
this.schema = modelSchema;
|
|
17
|
+
}
|
|
18
|
+
validate(model) {
|
|
19
|
+
return this.schema
|
|
20
|
+
.validate(model, { abortEarly: false })
|
|
21
|
+
.then(() => {
|
|
22
|
+
return [];
|
|
23
|
+
})
|
|
24
|
+
.catch((e) => {
|
|
25
|
+
return e.inner.map((error) => ({ path: error.path, type: error.type, message: error.message }));
|
|
26
|
+
});
|
|
27
|
+
}
|
|
20
28
|
}
|
|
21
29
|
|
|
22
|
-
const SCHEMA_METADATA_NAMESPACE = 'custom:muziehdesign:
|
|
30
|
+
const SCHEMA_METADATA_NAMESPACE = 'custom:muziehdesign:annotations';
|
|
23
31
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
const METADATA_KEY = 'custom:muziehdesign:annotations';
|
|
33
|
+
var ConstraintType;
|
|
34
|
+
(function (ConstraintType) {
|
|
35
|
+
ConstraintType[ConstraintType["string"] = 0] = "string";
|
|
36
|
+
ConstraintType[ConstraintType["boolean"] = 1] = "boolean";
|
|
37
|
+
ConstraintType[ConstraintType["date"] = 2] = "date";
|
|
38
|
+
ConstraintType[ConstraintType["object"] = 3] = "object";
|
|
39
|
+
ConstraintType[ConstraintType["number"] = 4] = "number";
|
|
40
|
+
})(ConstraintType || (ConstraintType = {}));
|
|
41
|
+
const registerMetadata = (target, propertyKey, constraint) => {
|
|
42
|
+
const metadata = Reflect.getMetadata(METADATA_KEY, target) || new Map();
|
|
43
|
+
metadata.set(propertyKey, constraint);
|
|
44
|
+
Reflect.defineMetadata(METADATA_KEY, metadata, target);
|
|
45
|
+
};
|
|
46
|
+
function StringType(...annotations) {
|
|
47
|
+
return function (target, propertyKey) {
|
|
48
|
+
const o = Object.assign({}, ...annotations);
|
|
49
|
+
o.constraintType = ConstraintType.string;
|
|
50
|
+
registerMetadata(target, propertyKey, o);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function BooleanType(...annotations) {
|
|
54
|
+
return function (target, propertyKey) {
|
|
55
|
+
const o = Object.assign({}, ...annotations);
|
|
56
|
+
o.constraintType = ConstraintType.boolean;
|
|
57
|
+
registerMetadata(target, propertyKey, o);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function DateType(...annotations) {
|
|
61
|
+
return function (target, propertyKey) {
|
|
62
|
+
const o = Object.assign({}, ...annotations);
|
|
63
|
+
o.constraintType = ConstraintType.date;
|
|
64
|
+
registerMetadata(target, propertyKey, o);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function NumberType(...annotations) {
|
|
68
|
+
return function (target, propertyKey) {
|
|
69
|
+
const o = Object.assign({}, ...annotations);
|
|
70
|
+
o.constraintType = ConstraintType.number;
|
|
71
|
+
registerMetadata(target, propertyKey, o);
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function ObjectType(type, ...annotations) {
|
|
75
|
+
return function (target, propertyKey) {
|
|
76
|
+
const o = Object.assign({}, ...annotations, { getInstance: () => new type() });
|
|
77
|
+
o.constraintType = ConstraintType.object;
|
|
78
|
+
registerMetadata(target, propertyKey, o);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function required(message) {
|
|
82
|
+
return { required: { required: true, message: message } };
|
|
83
|
+
}
|
|
84
|
+
function pattern(regex, message) {
|
|
85
|
+
return { pattern: { pattern: regex, message: message } };
|
|
86
|
+
}
|
|
87
|
+
function length(length, message) {
|
|
88
|
+
return { length: { length: length, message: message } };
|
|
89
|
+
}
|
|
90
|
+
function maxLength(maxLength, message) {
|
|
91
|
+
return { maxLength: { maxLength: maxLength, message: message } };
|
|
92
|
+
}
|
|
93
|
+
function minLength(minLength, message) {
|
|
94
|
+
return { minLength: { minLength: minLength, message: message } };
|
|
95
|
+
}
|
|
96
|
+
function ofValues(values, message) {
|
|
97
|
+
return { ofValues: { values: values, message: message } };
|
|
98
|
+
}
|
|
99
|
+
function equals(value, message) {
|
|
100
|
+
return { equals: { equals: value, message: message } };
|
|
101
|
+
}
|
|
102
|
+
function min(value, message) {
|
|
103
|
+
return { min: { min: value, message: message } };
|
|
104
|
+
}
|
|
105
|
+
function max(value, message) {
|
|
106
|
+
return { max: { max: value, message: message } };
|
|
107
|
+
}
|
|
108
|
+
function test(name, test, message) {
|
|
109
|
+
return { test: { name: name, test: test, message: message } };
|
|
110
|
+
}
|
|
31
111
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
112
|
+
/*
|
|
113
|
+
Schema rules need to be built in the order they need to be evaluated in.
|
|
114
|
+
For example,
|
|
115
|
+
```
|
|
116
|
+
schema.required().max(...).matches(...);
|
|
117
|
+
```
|
|
118
|
+
evaluates the 3 rules in this order
|
|
119
|
+
- required
|
|
120
|
+
- max
|
|
121
|
+
- matches
|
|
122
|
+
*/
|
|
123
|
+
class ModelSchemaFactory {
|
|
124
|
+
constructor() { }
|
|
125
|
+
build(model) {
|
|
126
|
+
const schema = this.buildObjectSchema(model);
|
|
127
|
+
return new ModelValidator(schema);
|
|
128
|
+
}
|
|
129
|
+
buildObjectSchema(model) {
|
|
130
|
+
const metadata = Reflect.getMetadata(SCHEMA_METADATA_NAMESPACE, model);
|
|
131
|
+
let shape = {};
|
|
132
|
+
metadata.forEach((value, key) => {
|
|
133
|
+
if (value.constraintType == ConstraintType.string) {
|
|
134
|
+
shape[key] = this.buildStringSchema(value);
|
|
135
|
+
}
|
|
136
|
+
else if (value.constraintType == ConstraintType.boolean) {
|
|
137
|
+
shape[key] = this.buildBooleanSchema(value);
|
|
138
|
+
}
|
|
139
|
+
else if (value.constraintType == ConstraintType.date) {
|
|
140
|
+
shape[key] = this.buildDateSchema(value);
|
|
141
|
+
}
|
|
142
|
+
else if (value.constraintType == ConstraintType.object) {
|
|
143
|
+
shape[key] = this.buildNestedObjectSchema(value);
|
|
144
|
+
}
|
|
145
|
+
else if (value.constraintType == ConstraintType.number) {
|
|
146
|
+
shape[key] = this.buildNumberSchema(value);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
return object(shape);
|
|
150
|
+
}
|
|
151
|
+
buildStringSchema(options) {
|
|
152
|
+
let schema = Yup.string();
|
|
153
|
+
if (options.required) {
|
|
154
|
+
schema = schema.required(options.required.message);
|
|
155
|
+
}
|
|
156
|
+
if (options.length) {
|
|
157
|
+
schema = schema.length(options.length.length, options.length.message);
|
|
158
|
+
}
|
|
159
|
+
if (options.maxLength) {
|
|
160
|
+
schema = schema.max(options.maxLength.maxLength, options.maxLength.message);
|
|
161
|
+
}
|
|
162
|
+
if (options.minLength) {
|
|
163
|
+
schema = schema.min(options.minLength.minLength, options.minLength.message);
|
|
164
|
+
}
|
|
165
|
+
if (options.pattern) {
|
|
166
|
+
schema = schema.matches(options.pattern.pattern, { message: options.pattern.message, excludeEmptyString: true });
|
|
167
|
+
}
|
|
168
|
+
return schema;
|
|
169
|
+
}
|
|
170
|
+
buildBooleanSchema(options) {
|
|
171
|
+
let schema = Yup.boolean();
|
|
172
|
+
if (options.required) {
|
|
173
|
+
schema = schema.required(options.required.message);
|
|
174
|
+
}
|
|
175
|
+
if (options.equals) {
|
|
176
|
+
if (options.equals.equals) {
|
|
177
|
+
schema = schema.isTrue(options.equals.message);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
schema = schema.isFalse(options.equals.message);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return schema;
|
|
184
|
+
}
|
|
185
|
+
buildDateSchema(options) {
|
|
186
|
+
let schema = Yup.date();
|
|
187
|
+
if (options.required) {
|
|
188
|
+
schema = schema.required(options.required.message);
|
|
189
|
+
}
|
|
190
|
+
if (options.min) {
|
|
191
|
+
schema = schema.min(options.min.min, options.min.message);
|
|
192
|
+
}
|
|
193
|
+
if (options.max) {
|
|
194
|
+
schema = schema.max(options.max.max, options.max.message);
|
|
195
|
+
}
|
|
196
|
+
if (options.test) {
|
|
197
|
+
schema = schema.test({
|
|
198
|
+
name: options.test.name,
|
|
199
|
+
message: options.test.message,
|
|
200
|
+
test: (d, context) => {
|
|
201
|
+
return options.test.test(d);
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return schema;
|
|
206
|
+
}
|
|
207
|
+
buildNumberSchema(options) {
|
|
208
|
+
let schema = Yup.number();
|
|
209
|
+
if (options.required) {
|
|
210
|
+
schema = schema.required(options.required.message);
|
|
211
|
+
}
|
|
212
|
+
return schema;
|
|
213
|
+
}
|
|
214
|
+
buildNestedObjectSchema(options) {
|
|
215
|
+
let nestedSchema = this.buildObjectSchema(options.getInstance());
|
|
216
|
+
if (options.required) {
|
|
217
|
+
nestedSchema = nestedSchema.required();
|
|
218
|
+
}
|
|
219
|
+
return nestedSchema;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
ModelSchemaFactory.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: ModelSchemaFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
223
|
+
ModelSchemaFactory.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: ModelSchemaFactory, providedIn: 'root' });
|
|
224
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: ModelSchemaFactory, decorators: [{
|
|
225
|
+
type: Injectable,
|
|
226
|
+
args: [{
|
|
227
|
+
providedIn: 'root',
|
|
228
|
+
}]
|
|
82
229
|
}], ctorParameters: function () { return []; } });
|
|
83
230
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
options: options,
|
|
91
|
-
});
|
|
92
|
-
// TODO: define on property
|
|
93
|
-
Reflect.defineMetadata('custom:muziehdesign:schema', metadata, target);
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
function BooleanSchema(options) {
|
|
97
|
-
return function (target, propertyKey) {
|
|
98
|
-
const metadata = Reflect.getMetadata('custom:muziehdesign:schema', target) || [];
|
|
99
|
-
metadata.push({
|
|
100
|
-
name: propertyKey,
|
|
101
|
-
type: PropertyType.boolean,
|
|
102
|
-
options: options,
|
|
103
|
-
});
|
|
104
|
-
// TODO: define on property
|
|
105
|
-
Reflect.defineMetadata('custom:muziehdesign:schema', metadata, target);
|
|
106
|
-
};
|
|
231
|
+
class FieldErrorsComponent {
|
|
232
|
+
get errorMessage() {
|
|
233
|
+
var _a, _b;
|
|
234
|
+
const errorKeys = Object.keys(((_a = this.field) === null || _a === void 0 ? void 0 : _a.errors) || {});
|
|
235
|
+
return errorKeys.length > 0 ? (_b = this.field) === null || _b === void 0 ? void 0 : _b.errors[errorKeys[0]] : '';
|
|
236
|
+
}
|
|
107
237
|
}
|
|
238
|
+
FieldErrorsComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FieldErrorsComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
|
239
|
+
FieldErrorsComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "14.0.5", type: FieldErrorsComponent, selector: "mz-field-errors", inputs: { field: "field" }, ngImport: i0, template: "<div class=\"field-error\" *ngIf=\"errorMessage\">{{ errorMessage }}</div>\n", styles: [""], dependencies: [{ kind: "directive", type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }] });
|
|
240
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FieldErrorsComponent, decorators: [{
|
|
241
|
+
type: Component,
|
|
242
|
+
args: [{ selector: 'mz-field-errors', template: "<div class=\"field-error\" *ngIf=\"errorMessage\">{{ errorMessage }}</div>\n" }]
|
|
243
|
+
}], propDecorators: { field: [{
|
|
244
|
+
type: Input
|
|
245
|
+
}] } });
|
|
108
246
|
|
|
109
|
-
class FormsModule {
|
|
110
|
-
}
|
|
111
|
-
FormsModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
112
|
-
FormsModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.0.5", ngImport: i0, type: FormsModule
|
|
113
|
-
|
|
114
|
-
i0.ɵɵ
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
247
|
+
class FormsModule {
|
|
248
|
+
}
|
|
249
|
+
FormsModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
250
|
+
FormsModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, declarations: [FieldErrorsComponent], imports: [CommonModule // TODO: can remove once done with temp error displaying
|
|
251
|
+
], exports: [FieldErrorsComponent] });
|
|
252
|
+
FormsModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, imports: [CommonModule // TODO: can remove once done with temp error displaying
|
|
253
|
+
] });
|
|
254
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: FormsModule, decorators: [{
|
|
255
|
+
type: NgModule,
|
|
256
|
+
args: [{
|
|
257
|
+
providers: [],
|
|
258
|
+
declarations: [
|
|
259
|
+
FieldErrorsComponent
|
|
260
|
+
],
|
|
261
|
+
exports: [
|
|
262
|
+
FieldErrorsComponent
|
|
263
|
+
],
|
|
264
|
+
imports: [
|
|
265
|
+
CommonModule // TODO: can remove once done with temp error displaying
|
|
266
|
+
]
|
|
267
|
+
}]
|
|
119
268
|
}] });
|
|
120
269
|
|
|
121
|
-
|
|
122
|
-
|
|
270
|
+
class NgFormModelState {
|
|
271
|
+
constructor(form, modelValidator, options) {
|
|
272
|
+
this.form = form;
|
|
273
|
+
this.modelValidator = modelValidator;
|
|
274
|
+
this.options = options;
|
|
275
|
+
this.changesSubject = new BehaviorSubject(undefined);
|
|
276
|
+
this.changes = this.changesSubject.asObservable();
|
|
277
|
+
this.form.form.valueChanges
|
|
278
|
+
.pipe(switchMap((x) => __awaiter(this, void 0, void 0, function* () {
|
|
279
|
+
return from(this.validate());
|
|
280
|
+
})))
|
|
281
|
+
.subscribe();
|
|
282
|
+
}
|
|
283
|
+
getCurrent() {
|
|
284
|
+
return this.changesSubject.value;
|
|
285
|
+
}
|
|
286
|
+
setState(model, errors) {
|
|
287
|
+
const state = {
|
|
288
|
+
model: model,
|
|
289
|
+
errors: errors,
|
|
290
|
+
valid: errors.length === 0,
|
|
291
|
+
};
|
|
292
|
+
this.setStateInternal(state);
|
|
293
|
+
}
|
|
294
|
+
setErrors(errors) {
|
|
295
|
+
//this.errors.next(errors);
|
|
296
|
+
throw new Error('needs implementation');
|
|
297
|
+
}
|
|
298
|
+
appendErrors(errors, skipEmit) {
|
|
299
|
+
var _a;
|
|
300
|
+
const allErrors = [...(((_a = this.changesSubject.value) === null || _a === void 0 ? void 0 : _a.errors) || []), ...errors];
|
|
301
|
+
const state = Object.assign({}, this.changesSubject.value);
|
|
302
|
+
state.errors = allErrors;
|
|
303
|
+
this.setStateInternal(state);
|
|
304
|
+
}
|
|
305
|
+
validate() {
|
|
306
|
+
var _a;
|
|
307
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
308
|
+
const model = this.form.value;
|
|
309
|
+
const state = yield this.runValidations(model, (_a = this.options) === null || _a === void 0 ? void 0 : _a.onValidate);
|
|
310
|
+
this.setStateInternal(state);
|
|
311
|
+
return state;
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
setStateInternal(state) {
|
|
315
|
+
this.deleteFormErrors();
|
|
316
|
+
const grouped = state.errors.reduce((grouped, v) => grouped.set(v.path, [...(grouped.get(v.path) || []), v]), new Map());
|
|
317
|
+
grouped.forEach((value, path) => {
|
|
318
|
+
let validationErrors = {};
|
|
319
|
+
value.forEach((v) => (validationErrors[v.type] = v.message));
|
|
320
|
+
const control = this.form.form.get(path);
|
|
321
|
+
if (!control) {
|
|
322
|
+
// TODO: use actual logging service
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
control.setErrors(validationErrors);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
this.changesSubject.next(state);
|
|
329
|
+
}
|
|
330
|
+
deleteFormErrors() {
|
|
331
|
+
this.deleteErrors(this.form.form);
|
|
332
|
+
}
|
|
333
|
+
deleteErrors(rootFormGroup) {
|
|
334
|
+
Object.keys(rootFormGroup.controls).forEach((key) => {
|
|
335
|
+
const control = rootFormGroup.controls[key];
|
|
336
|
+
if (!this.isParentControl(control)) {
|
|
337
|
+
this.deleteErrorsFromControl(key, control);
|
|
338
|
+
}
|
|
339
|
+
else if (control instanceof FormGroup) {
|
|
340
|
+
this.deleteErrors(control);
|
|
341
|
+
}
|
|
342
|
+
else if (control instanceof FormArray) {
|
|
343
|
+
this.loopFormArray(control);
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
deleteErrorsFromControl(key, control) {
|
|
348
|
+
control.setErrors(null);
|
|
349
|
+
}
|
|
350
|
+
isParentControl(control) {
|
|
351
|
+
return control instanceof FormGroup || control instanceof FormArray;
|
|
352
|
+
}
|
|
353
|
+
loopFormArray(formArray) {
|
|
354
|
+
formArray.controls.forEach((control, index) => {
|
|
355
|
+
if (!this.isParentControl(control)) {
|
|
356
|
+
this.deleteErrorsFromControl(index, control);
|
|
357
|
+
}
|
|
358
|
+
else if (control instanceof FormGroup) {
|
|
359
|
+
this.deleteErrors(control);
|
|
360
|
+
}
|
|
361
|
+
else if (control instanceof FormArray) {
|
|
362
|
+
this.loopFormArray(control);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
runValidations(model, callback) {
|
|
367
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
368
|
+
const list = yield this.modelValidator.validate(model);
|
|
369
|
+
const final = (callback === null || callback === void 0 ? void 0 : callback(list)) || list;
|
|
370
|
+
return {
|
|
371
|
+
valid: final.length === 0,
|
|
372
|
+
errors: final,
|
|
373
|
+
model: model,
|
|
374
|
+
};
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
class NgFormModelStateFactory {
|
|
380
|
+
constructor(factory) {
|
|
381
|
+
this.factory = factory;
|
|
382
|
+
}
|
|
383
|
+
create(form, model, options) {
|
|
384
|
+
const modelState = new NgFormModelState(form, this.factory.build(model), options);
|
|
385
|
+
return modelState;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
NgFormModelStateFactory.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: NgFormModelStateFactory, deps: [{ token: ModelSchemaFactory }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
389
|
+
NgFormModelStateFactory.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: NgFormModelStateFactory, providedIn: 'root' });
|
|
390
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.5", ngImport: i0, type: NgFormModelStateFactory, decorators: [{
|
|
391
|
+
type: Injectable,
|
|
392
|
+
args: [{
|
|
393
|
+
providedIn: 'root',
|
|
394
|
+
}]
|
|
395
|
+
}], ctorParameters: function () { return [{ type: ModelSchemaFactory }]; } });
|
|
396
|
+
|
|
397
|
+
const dateMaskOptions = {
|
|
398
|
+
mask: 'MM/dd/yyyy',
|
|
399
|
+
format: (date) => {
|
|
400
|
+
if (!date) {
|
|
401
|
+
return '';
|
|
402
|
+
}
|
|
403
|
+
return format(date, 'MM/dd/yyyy');
|
|
404
|
+
},
|
|
405
|
+
parse: (str) => {
|
|
406
|
+
const dateRegex = /(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/([12]\d{3})/;
|
|
407
|
+
if (!str.match(dateRegex)) {
|
|
408
|
+
return undefined;
|
|
409
|
+
}
|
|
410
|
+
const parsed = parse(str, 'MM/dd/yyyy', new Date());
|
|
411
|
+
if (isValid(parsed)) {
|
|
412
|
+
return parsed;
|
|
413
|
+
}
|
|
414
|
+
return undefined;
|
|
415
|
+
},
|
|
416
|
+
blocks: {
|
|
417
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
418
|
+
MM: {
|
|
419
|
+
mask: IMask.MaskedRange,
|
|
420
|
+
from: 1,
|
|
421
|
+
to: 12,
|
|
422
|
+
},
|
|
423
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
424
|
+
dd: {
|
|
425
|
+
mask: IMask.MaskedRange,
|
|
426
|
+
from: 1,
|
|
427
|
+
to: 31,
|
|
428
|
+
},
|
|
429
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
430
|
+
yyyy: {
|
|
431
|
+
mask: IMask.MaskedRange,
|
|
432
|
+
from: 1000,
|
|
433
|
+
to: 9999,
|
|
434
|
+
},
|
|
435
|
+
},
|
|
436
|
+
};
|
|
437
|
+
const phoneNumberOptions = {
|
|
438
|
+
mask: '(000) 000-0000',
|
|
439
|
+
};
|
|
440
|
+
const currencyOptions = {
|
|
441
|
+
mask: Number,
|
|
442
|
+
scale: 2,
|
|
443
|
+
thousandsSeparator: ',',
|
|
444
|
+
padFractionalZeros: true,
|
|
445
|
+
radix: '.',
|
|
446
|
+
mapToRadix: ['.'],
|
|
447
|
+
};
|
|
448
|
+
const integerOptions = {
|
|
449
|
+
mask: Number,
|
|
450
|
+
scale: 0,
|
|
451
|
+
thousandsSeparator: ',',
|
|
452
|
+
padFractionalZeros: true,
|
|
453
|
+
radix: '.',
|
|
454
|
+
mapToRadix: ['.'],
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
/*
|
|
458
|
+
* Public API Surface of forms
|
|
123
459
|
*/
|
|
124
460
|
|
|
125
|
-
/**
|
|
126
|
-
* Generated bundle index. Do not edit.
|
|
461
|
+
/**
|
|
462
|
+
* Generated bundle index. Do not edit.
|
|
127
463
|
*/
|
|
128
464
|
|
|
129
|
-
export {
|
|
465
|
+
export { BooleanType, ConstraintType, DateType, FieldErrorsComponent, FormsModule, ModelSchemaFactory, ModelValidator, NgFormModelState, NgFormModelStateFactory, NumberType, ObjectType, StringType, currencyOptions, dateMaskOptions, equals, integerOptions, length, max, maxLength, min, minLength, ofValues, pattern, phoneNumberOptions, required, test };
|
|
130
466
|
//# sourceMappingURL=muziehdesign-forms.mjs.map
|