1212 :key =" control.name"
1313 :control =" control"
1414 :submited =" submited"
15+ :forceValidation =" forceValidation"
1516 @change =" valueChange"
1617 @blur =" onBlur"
1718 @validate =" onValidate"
3637</template >
3738
3839<script lang="ts">
40+ import { nextTick } from ' vue' ;
41+
3942import {
4043 defineComponent ,
4144 PropType ,
@@ -61,6 +64,7 @@ import {
6164} from ' @/core/models' ;
6265import { dynamicFormsSymbol } from ' @/useApi' ;
6366import { deepClone , hasValue , removeEmpty } from ' @/core/utils/helpers' ;
67+ import { FieldControl } from ' @/core/factories' ;
6468
6569const props = {
6670 form: {
@@ -73,12 +77,6 @@ const components = {
7377 DynamicInput ,
7478};
7579
76- const EMPTY_CONTROL = {
77- dirty: false ,
78- touched: false ,
79- valid: true ,
80- };
81-
8280/* const AVAILABLE_THEMES = ['default', 'material'];
8381 */
8482export default defineComponent ({
@@ -90,7 +88,7 @@ export default defineComponent({
9088 const cache = deepClone (toRaw (props .form .fields ));
9189
9290 const controls: Ref <FormControl <InputType >[]> = ref ([]);
93- const submited = ref (false );
91+ const forceValidation = ref (false );
9492
9593 const deNormalizedScopedSlots = computed (() => Object .keys (ctx .slots ));
9694
@@ -168,17 +166,15 @@ export default defineComponent({
168166 Object .entries (props .form ?.fields ).map (
169167 ([key , field ]: [string , InputType ]) =>
170168 empty
171- ? ({
169+ ? FieldControl ({
172170 ... field ,
173171 name: key ,
174172 value: null ,
175- ... EMPTY_CONTROL ,
176- } as FormControl <InputType >)
177- : ({
173+ })
174+ : FieldControl ({
178175 ... field ,
179176 name: key ,
180- ... EMPTY_CONTROL ,
181- } as FormControl <InputType >),
177+ }),
182178 ) || [];
183179 if (props .form .fieldOrder ) {
184180 controls .value = controlArray .sort (
@@ -225,61 +221,49 @@ export default defineComponent({
225221 }
226222 }
227223
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-
256224 function detectChanges(fields ) {
257225 const changes = diff (cache , deepClone (fields ));
258226 Object .entries (changes ).forEach (([key , value ]) => {
259227 let ctrl = findControlByName (key );
260228 if (ctrl ) {
261229 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+ }
263240 });
264241 }
265242 });
266243 }
267244
268245 function resetForm() {
269246 mapControls (true );
247+ forceValidation .value = false ;
270248 }
271249
272- function handleSubmit() {
273- submited .value = true ;
250+ async function handleSubmit() {
251+ validateAll ();
252+
253+ await nextTick ();
274254
275255 if (isValid .value ) {
276- ctx .emit (' submited ' , formValues );
256+ ctx .emit (' submitted ' , formValues );
277257 resetForm ();
278258 } else {
279259 ctx .emit (' error' , formValues );
280260 }
281261 }
282262
263+ function validateAll() {
264+ forceValidation .value = true ;
265+ }
266+
283267 watch (
284268 () => props .form .fields ,
285269 fields => {
@@ -303,10 +287,11 @@ export default defineComponent({
303287 errors ,
304288 deNormalizedScopedSlots ,
305289 normalizedControls ,
306- submited ,
307290 formattedOptions ,
308291 onBlur ,
309292 onValidate ,
293+ forceValidation ,
294+ validateAll ,
310295 };
311296 },
312297});
0 commit comments