Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 8c490af

Browse files
committed
fix(validations): force validation and minor fixes
1 parent cedf3d2 commit 8c490af

File tree

11 files changed

+59
-32
lines changed

11 files changed

+59
-32
lines changed

dev/typescript/App.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<h1 class="title mb-16 text-bg">{{ title }}</h1>
1111
<dynamic-form
1212
:form="form"
13-
@submited="handleSubmit"
13+
@submitted="handleSubmit"
1414
@change="valueChanged"
1515
@error="handleError"
1616
>
@@ -34,7 +34,7 @@
3434
</template>
3535
</dynamic-form>
3636
<button
37-
class="btn bg-teal-500 text-white hover:bg-teal-700 mt-4"
37+
class="btn bg-green-500 text-white hover:bg-green-700 mt-4"
3838
submit="true"
3939
:form="form?.id"
4040
>

src/components/checkbox-input/CheckboxInput.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { useInputValidation } from '@/composables/use-validation';
66
77
const props = {
88
control: Object as PropType<FormControl<CheckboxInput>>,
9+
forceValidation: {
10+
type: Boolean,
11+
default: false,
12+
},
913
};
1014
1115
export default defineComponent({

src/components/dynamic-form/DynamicForm.vue

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
:key="control.name"
1313
:control="control"
1414
:submited="submited"
15+
:forceValidation="forceValidation"
1516
@change="valueChange"
1617
@blur="onBlur"
1718
@validate="onValidate"
@@ -36,6 +37,8 @@
3637
</template>
3738

3839
<script lang="ts">
40+
import { nextTick } from 'vue';
41+
3942
import {
4043
defineComponent,
4144
PropType,
@@ -85,7 +88,7 @@ export default defineComponent({
8588
const cache = deepClone(toRaw(props.form.fields));
8689
8790
const controls: Ref<FormControl<InputType>[]> = ref([]);
88-
const submited = ref(false);
91+
const forceValidation = ref(false);
8992
9093
const deNormalizedScopedSlots = computed(() => Object.keys(ctx.slots));
9194
@@ -241,19 +244,26 @@ export default defineComponent({
241244
242245
function resetForm() {
243246
mapControls(true);
247+
forceValidation.value = false;
244248
}
245249
246-
function handleSubmit() {
247-
submited.value = true;
250+
async function handleSubmit() {
251+
validateAll();
252+
253+
await nextTick();
248254
249255
if (isValid.value) {
250-
ctx.emit('submited', formValues);
256+
ctx.emit('submitted', formValues);
251257
resetForm();
252258
} else {
253259
ctx.emit('error', formValues);
254260
}
255261
}
256262
263+
function validateAll() {
264+
forceValidation.value = true;
265+
}
266+
257267
watch(
258268
() => props.form.fields,
259269
fields => {
@@ -277,10 +287,11 @@ export default defineComponent({
277287
errors,
278288
deNormalizedScopedSlots,
279289
normalizedControls,
280-
submited,
281290
formattedOptions,
282291
onBlur,
283292
onValidate,
293+
forceValidation,
294+
validateAll,
284295
};
285296
},
286297
});

src/components/dynamic-input/DynamicInput.vue

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ import {
2828
InputEvent,
2929
} from '@/core/models';
3030
31-
import { values, isArray, isObject } from '@/core/utils/helpers';
31+
import { isArray, isObject } from '@/core/utils/helpers';
3232
import { useInputEvents } from '@/composables/input-events';
33-
import { dynamicFormsSymbol } from '@/useApi';
3433
3534
const components = {
3635
TextInputComponent,
@@ -46,9 +45,9 @@ const props = {
4645
type: Object as PropType<FormControl<InputType>>,
4746
required: true,
4847
},
49-
submited: {
48+
forceValidation: {
5049
type: Boolean,
51-
required: true,
50+
default: false,
5251
},
5352
};
5453
@@ -65,7 +64,6 @@ export default defineComponent({
6564
props,
6665
setup(props, { emit, slots }) {
6766
const { onFocus, onBlur } = useInputEvents(props?.control, emit);
68-
const { options } = inject(dynamicFormsSymbol);
6967
7068
let component;
7169
@@ -78,6 +76,7 @@ export default defineComponent({
7876
onFocus: (e: InputEvent) => emit('focus', e),
7977
onValidate: (validation: ValidationEvent) =>
8078
emit('validate', validation),
79+
forceValidation: props.forceValidation,
8180
};
8281
});
8382
@@ -105,18 +104,6 @@ export default defineComponent({
105104
return [classes, props?.control?.customClass];
106105
});
107106
108-
const autoValidate = computed(
109-
() => props?.control?.touched && options?.autoValidate,
110-
);
111-
112-
const errorMessages = computed(() => {
113-
const errors = values(props?.control?.errors || {});
114-
if (errors.length > 0 && (props.submited || autoValidate.value)) {
115-
return errors.map(value => value.text);
116-
}
117-
return [];
118-
});
119-
120107
function valueChange($event) {
121108
emit('change', $event);
122109
}

src/components/number-input/NumberInput.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { useInputValidation } from '@/composables/use-validation';
66
77
const props = {
88
control: Object as PropType<FormControl<NumberInput>>,
9+
forceValidation: {
10+
type: Boolean,
11+
default: false,
12+
},
913
};
1014
1115
export default defineComponent({

src/components/radio-input/RadioInput.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { useInputValidation } from '@/composables/use-validation';
66
77
const props = {
88
control: Object as PropType<FormControl<RadioInput>>,
9+
forceValidation: {
10+
type: Boolean,
11+
default: false,
12+
},
913
};
1014
1115
export default defineComponent({

src/components/select-input/SelectInput.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { useInputValidation } from '@/composables/use-validation';
77
88
const props = {
99
control: Object as PropType<FormControl<SelectInput>>,
10+
forceValidation: {
11+
type: Boolean,
12+
default: false,
13+
},
1014
};
1115
1216
export default defineComponent({

src/components/text-area-input/TextAreaInput.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { useInputEvents } from '@/composables/input-events';
55
import { useInputValidation } from '@/composables/use-validation';
66
const props = {
77
control: Object as PropType<FormControl<TextAreaInput>>,
8+
forceValidation: {
9+
type: Boolean,
10+
default: false,
11+
},
812
};
913
1014
export default defineComponent({

src/components/text-input/TextInput.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const props = {
1515
control: Object as PropType<
1616
FormControl<TextInput | EmailInput | PasswordInput | UrlInput | ColorInput>
1717
>,
18+
forceValidation: {
19+
type: Boolean,
20+
default: false,
21+
},
1822
};
1923
2024
export default defineComponent({

src/composables/use-validation.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import { ErrorMessage } from '@/core/models';
44
import { removeEmpty } from '@/core/utils/helpers';
5-
import { computed, ref } from 'vue';
5+
import { computed, ref, watch } from 'vue';
66

77
export function useInputValidation(props: any, emit: any) {
88
const isPendingValidation = ref(false);
9+
910
const isRequired = computed(() => {
1011
return props.control.validations.some(
1112
validation => validation.type === 'required',
@@ -16,9 +17,9 @@ export function useInputValidation(props: any, emit: any) {
1617
return props.control.validations.length > 0;
1718
});
1819

19-
async function validate(): Promise<void> {
20+
async function validate(force = false): Promise<void> {
2021
if (
21-
(props.control.touched || props.control.dirty) &&
22+
force || (props.control.touched || props.control.dirty) &&
2223
requiresValidation.value
2324
) {
2425
let errors = {};
@@ -36,10 +37,6 @@ export function useInputValidation(props: any, emit: any) {
3637
}
3738
});
3839

39-
console.log({
40-
sync: syncValidations,
41-
async: asyncValidations,
42-
});
4340
if(asyncValidations.length > 0) {
4441
isPendingValidation.value = true;
4542

@@ -112,6 +109,15 @@ export function useInputValidation(props: any, emit: any) {
112109
];
113110
});
114111

112+
watch(
113+
() => props.forceValidation,
114+
value => {
115+
if(value) {
116+
validate(value)
117+
}
118+
},
119+
);
120+
115121
return {
116122
isPendingValidation,
117123
validate,

src/styles/themes/default.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ $input-error-color: #dc3545 !default;
5252

5353
.form-group {
5454
display: flex;
55-
width: 100%;
5655
flex-direction: column;
5756
margin-bottom: 1rem;
5857

0 commit comments

Comments
 (0)