12
12
:key =" control.name"
13
13
:control =" control"
14
14
:submited =" submited"
15
+ :forceValidation =" forceValidation"
15
16
@change =" valueChange"
16
17
@blur =" onBlur"
17
18
@validate =" onValidate"
36
37
</template >
37
38
38
39
<script lang="ts">
40
+ import { nextTick } from ' vue' ;
41
+
39
42
import {
40
43
defineComponent ,
41
44
PropType ,
@@ -61,6 +64,7 @@ import {
61
64
} from ' @/core/models' ;
62
65
import { dynamicFormsSymbol } from ' @/useApi' ;
63
66
import { deepClone , hasValue , removeEmpty } from ' @/core/utils/helpers' ;
67
+ import { FieldControl } from ' @/core/factories' ;
64
68
65
69
const props = {
66
70
form: {
@@ -73,12 +77,6 @@ const components = {
73
77
DynamicInput ,
74
78
};
75
79
76
- const EMPTY_CONTROL = {
77
- dirty: false ,
78
- touched: false ,
79
- valid: true ,
80
- };
81
-
82
80
/* const AVAILABLE_THEMES = ['default', 'material'];
83
81
*/
84
82
export default defineComponent ({
@@ -90,7 +88,7 @@ export default defineComponent({
90
88
const cache = deepClone (toRaw (props .form .fields ));
91
89
92
90
const controls: Ref <FormControl <InputType >[]> = ref ([]);
93
- const submited = ref (false );
91
+ const forceValidation = ref (false );
94
92
95
93
const deNormalizedScopedSlots = computed (() => Object .keys (ctx .slots ));
96
94
@@ -168,17 +166,15 @@ export default defineComponent({
168
166
Object .entries (props .form ?.fields ).map (
169
167
([key , field ]: [string , InputType ]) =>
170
168
empty
171
- ? ({
169
+ ? FieldControl ({
172
170
... field ,
173
171
name: key ,
174
172
value: null ,
175
- ... EMPTY_CONTROL ,
176
- } as FormControl <InputType >)
177
- : ({
173
+ })
174
+ : FieldControl ({
178
175
... field ,
179
176
name: key ,
180
- ... EMPTY_CONTROL ,
181
- } as FormControl <InputType >),
177
+ }),
182
178
) || [];
183
179
if (props .form .fieldOrder ) {
184
180
controls .value = controlArray .sort (
@@ -225,61 +221,49 @@ export default defineComponent({
225
221
}
226
222
}
227
223
228
- /* function validateControl(control: FormControl<InputType>) {
229
- if (control.validations) {
230
- const validation = control.validations.reduce((prev, curr) => {
231
- const val =
232
- typeof curr.validator === 'function'
233
- ? curr.validator(control)
234
- : null;
235
- if (val !== null) {
236
- const [key, value] = Object.entries(val)[0];
237
- const obj = {};
238
- obj[key] = {
239
- value,
240
- text: curr.text,
241
- };
242
- return {
243
- ...prev,
244
- ...obj,
245
- };
246
- }
247
- return {
248
- ...prev,
249
- };
250
- }, {});
251
- control.errors = validation;
252
- control.valid = Object.keys(validation).length === 0;
253
- }
254
- } */
255
-
256
224
function detectChanges(fields ) {
257
225
const changes = diff (cache , deepClone (fields ));
258
226
Object .entries (changes ).forEach (([key , value ]) => {
259
227
let ctrl = findControlByName (key );
260
228
if (ctrl ) {
261
229
Object .entries (value ).forEach (([change , newValue ]) => {
262
- ctrl [change ] = newValue ;
230
+ if (change === ' options' || change === ' validations' ) {
231
+ Object .entries (newValue ).forEach (([optKey , optValue ]) => {
232
+ ctrl [change ][optKey ] = {
233
+ ... ctrl [change ][optKey ],
234
+ ... optValue ,
235
+ };
236
+ });
237
+ } else {
238
+ ctrl [change ] = newValue ;
239
+ }
263
240
});
264
241
}
265
242
});
266
243
}
267
244
268
245
function resetForm() {
269
246
mapControls (true );
247
+ forceValidation .value = false ;
270
248
}
271
249
272
- function handleSubmit() {
273
- submited .value = true ;
250
+ async function handleSubmit() {
251
+ validateAll ();
252
+
253
+ await nextTick ();
274
254
275
255
if (isValid .value ) {
276
- ctx .emit (' submited ' , formValues );
256
+ ctx .emit (' submitted ' , formValues );
277
257
resetForm ();
278
258
} else {
279
259
ctx .emit (' error' , formValues );
280
260
}
281
261
}
282
262
263
+ function validateAll() {
264
+ forceValidation .value = true ;
265
+ }
266
+
283
267
watch (
284
268
() => props .form .fields ,
285
269
fields => {
@@ -303,10 +287,11 @@ export default defineComponent({
303
287
errors ,
304
288
deNormalizedScopedSlots ,
305
289
normalizedControls ,
306
- submited ,
307
290
formattedOptions ,
308
291
onBlur ,
309
292
onValidate ,
293
+ forceValidation ,
294
+ validateAll ,
310
295
};
311
296
},
312
297
});
0 commit comments