Skip to content

Commit 333ed54

Browse files
[DEV] Working on validation more
1 parent 09ae8db commit 333ed54

File tree

9 files changed

+348
-168
lines changed

9 files changed

+348
-168
lines changed

src/plugin/VStepperForm.vue

Lines changed: 90 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@
1212
</h2>
1313
</v-col>
1414
</v-row>
15-
<!-- <v-row>
15+
<v-row>
1616
<v-col>
1717
stepperMode: {{ stepperModel }}
1818
<br />
1919
currentPageHasErrors: {{ currentPageHasErrors }}
2020
<br />
2121
errorPageIndexes: {{ errorPageIndexes }}
22-
<br />
23-
triggerValidation: {{ triggerValidation }}
2422
</v-col>
25-
</v-row> -->
23+
</v-row>
2624
</v-container>
2725

2826
<v-container
@@ -75,10 +73,9 @@
7573
:index="getIndex(i)"
7674
:page="page"
7775
:settings="settings"
78-
:triggerValidation="triggerValidation"
7976
:validateSchema="validateSchema"
8077
@next="validatePage(next)"
81-
@validate="onValidate"
78+
@validate="onValidate($event, next)"
8279
/>
8380
<PageReviewContainer
8481
v-else
@@ -138,10 +135,12 @@
138135
// import { VStepperVertical } from 'vuetify/labs/VStepperVertical';
139136
import { AllProps } from './utils/props';
140137
import { useDisplay } from 'vuetify';
141-
import {
138+
import type {
139+
EmitValidateEventPayload,
142140
Field,
143141
Page,
144142
Props,
143+
SchemaField,
145144
Settings,
146145
SummaryColumns,
147146
} from '@/plugin/types';
@@ -150,11 +149,15 @@ import {
150149
useStepperContainerClasses,
151150
} from './composables/classes';
152151
import componentEmits from './utils/emits';
152+
import { TriggerValidationBus } from './utils/globals';
153153
import { globalOptions } from './';
154154
import PageContainer from './components/shared/PageContainer.vue';
155155
import PageReviewContainer from './components/shared/PageReviewContainer.vue';
156156
import { useMergeProps } from './composables/helpers';
157-
import { watchDeep } from '@vueuse/core';
157+
import {
158+
useEventBus,
159+
watchDeep,
160+
} from '@vueuse/core';
158161
import {
159162
useGetValidationSchema,
160163
} from './composables/validation';
@@ -308,146 +311,118 @@ function headerItemDisabled(page: Page): boolean {
308311
}
309312
310313
311-
// & ------------------------------------------------ Validation //
312-
const validateSchema = useGetValidationSchema(allFieldsArray.value);
313-
const triggerValidation = ref(false);
314-
const fieldsHaveErrors = ref(false);
315-
316-
console.log('validateSchema', validateSchema);
317-
318-
319314
320-
// function validateButtonDisabled() {
321-
// const foo = JSON.parse(JSON.stringify(pages));
322-
// console.log('foo', foo);
323-
// fieldsHaveErrors.value = Object.values(pages).some((page: Page) => Object.values(page.fields).some((field: Field) => field.error));
324315
325-
// console.log('fieldsHaveErrors', fieldsHaveErrors.value);
316+
// & ------------------------------------------------ Validation //
317+
const validateSchema = useGetValidationSchema(allFieldsArray.value as SchemaField[]);
318+
const fieldsHaveErrors = ref(false);
319+
let triggerValidationBus: unknown | any;
326320
327-
// }
321+
onMounted(() => {
322+
triggerValidationBus = useEventBus<string>(TriggerValidationBus);
323+
});
328324
329325
330-
// TODO: This needs to be fixed //
331326
const currentPageHasErrors = ref(false);
332327
const errorPageIndexes = ref<number[]>([]);
333328
334-
function checkForPageErrors() {
335-
errorPageIndexes.value = [];
336329
337-
Object.values(pages).forEach((p: Page, i: number) => {
338-
const errorField = Object.values(p.fields).find((field: Field) => field.error);
339-
340-
if (errorField) {
341-
// eslint-disable-next-line no-param-reassign
342-
p.error = true;
343-
errorPageIndexes.value.push(i);
344-
return;
345-
}
330+
function setPageToError(pageIndex: EmitValidateEventPayload['pageIndex'], page?: Page): void {
331+
currentPageHasErrors.value = true;
346332
333+
if (page) {
347334
// eslint-disable-next-line no-param-reassign
348-
p.error = false;
349-
});
350-
351-
// Reset the error //
352-
const stepperPage = pages[stepperModel.value - 1];
353-
354-
if (stepperPage) {
355-
stepperPage.error = false;
335+
page.error = true;
356336
}
357337
358-
currentPageHasErrors.value = errorPageIndexes.value.includes(stepperModel.value - 1);
338+
if (!errorPageIndexes.value.includes(pageIndex)) {
339+
errorPageIndexes.value.push(pageIndex);
340+
}
359341
}
360342
361343
362-
// TODO: Figure out why this triggers twice for each field on the page //
363-
function onValidate(val) {
364-
console.group('onValidate');
365-
console.log(val);
344+
function onValidate(payload: EmitValidateEventPayload, event: () => void) {
345+
// console.group('onValidate');
346+
// console.log('payload', payload);
366347
367-
const { errors, fieldName } = val;
368-
let fieldHasErrors = false;
369-
370-
if (errors) {
371-
fieldHasErrors = errors[fieldName] || false;
372-
}
348+
const page = pages[payload.pageIndex];
373349
374-
const fieldPageIdx = Object.values(pages).findIndex((page: Page) => Object.values(page.fields).find((f: Field) => f.name === fieldName));
375-
const page = pages[fieldPageIdx];
376-
377-
console.log('page', page);
350+
// console.log('page', page);
378351
379352
if (!page) {
380-
console.groupEnd();
381-
382353
return;
383354
}
384355
385-
const field = page.fields.find((f: Field) => f.name === val.fieldName);
356+
const currentFieldIndex = page.fields.findIndex((f) => f.name === payload.fieldName);
386357
387-
console.log('field', field);
388-
389-
if (!field) {
390-
console.groupEnd();
358+
// Do not continue if field is not the last field //
359+
if (currentFieldIndex !== page.fields.length - 1) {
391360
return;
392361
}
393362
394-
console.log('fieldHasErrors', fieldHasErrors);
363+
let fieldsError = false;
395364
396-
field.error = fieldHasErrors ? true : false;
397-
page.error = field.error;
398-
currentPageHasErrors.value = field.error;
399-
fieldsHaveErrors.value = field.error;
365+
Object.values(page.fields).forEach((field: Field) => {
366+
if (field.error === true || !Object.prototype.hasOwnProperty.call(field, 'error')) {
367+
fieldsError = true;
368+
}
369+
});
400370
401-
console.log('pages', pages);
402371
403-
console.log(currentPageHasErrors.value);
372+
if (fieldsError) {
373+
// page.error = payload.error;
374+
setPageToError(payload.pageIndex);
375+
return;
376+
}
404377
405-
checkForPageErrors();
378+
if (payload.action === 'page') {
379+
page.error = payload.error === true;
406380
407-
// Resets so it can be triggered again //
408-
triggerValidation.value = false;
381+
if (page.error) {
382+
setPageToError(payload.pageIndex, page);
409383
410-
console.groupEnd();
384+
// console.groupEnd();
385+
return;
386+
}
411387
412-
// validateButtonDisabled();
388+
currentPageHasErrors.value = false;
389+
}
413390
414-
}
391+
const index = errorPageIndexes.value.indexOf(payload.pageIndex);
415392
393+
if (index > -1) {
394+
errorPageIndexes.value.splice(index, 1);
395+
}
416396
417397
418-
function onSubmit(val) {
419-
triggerValidation.value = true;
420-
checkForPageErrors();
398+
// debugger;
421399
422-
console.log('onSubmit', val);
423-
if (fieldsHaveErrors.value) {
400+
// const field = page.fields.find((f) => f.name === payload.fieldName);
401+
402+
if (fieldsError) {
403+
// console.log('XXXX fieldsError', fieldsError);
404+
// console.groupEnd();
424405
return;
425406
}
426-
// useValidation({
427-
// fields: allFieldsArray.value,
428-
// });
429-
}
430407
408+
currentPageHasErrors.value = false;
431409
432-
function validatePage(event: () => void): void {
433-
console.log('validatePage', event);
434-
triggerValidation.value = true;
410+
if (payload.nextPage && stepperModel.value === payload.pageIndex + 1) {
411+
nextPage(event);
412+
}
435413
436-
setTimeout(() => {
437-
// Resets so it can be triggered again //
438-
triggerValidation.value = false;
414+
// console.groupEnd();
415+
}
439416
440-
if (currentPageHasErrors.value) {
441-
return;
442-
}
443417
444-
nextPage(event);
445-
}, 250);
418+
function validatePage(event: () => void): void {
419+
console.log('validatePage', event);
446420
447-
// nextPage(event);
421+
triggerValidationBus.emit({
422+
pageIndex: stepperModel.value,
423+
});
448424
}
449425
450-
451426
// ------------------------------------------------ Callbacks //
452427
function callbacks() {
453428
whenCallback();
@@ -473,11 +448,24 @@ function whenCallback() {
473448
}
474449
475450
476-
// ------------------------------------------------ Submit Form //
451+
// -------------------------------------------------- Submit //
452+
function onSubmit(val) {
453+
// checkForPageErrors();
454+
455+
console.log('onSubmit', val);
456+
if (fieldsHaveErrors.value) {
457+
return;
458+
}
459+
// useValidation({
460+
// fields: allFieldsArray.value,
461+
// });
462+
}
463+
464+
477465
function submitForm() {
478-
console.log('VStepperForm submitForm');
466+
// console.log('VStepperForm submitForm');
479467
emit('submit');
480-
}
468+
};
481469
482470
483471
// ------------------------------------------------ Class & Styles //

0 commit comments

Comments
 (0)