From 61c73597b2e69c094e75c02476d825c5236710b5 Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Sat, 5 Jun 2021 06:52:09 +0200 Subject: [PATCH] fix: field with pre-register schema errors should be validated on register closes #3342 --- packages/vee-validate/src/useField.ts | 2 +- packages/vee-validate/src/useForm.ts | 66 +++++++++++++++--------- packages/vee-validate/tests/Form.spec.ts | 33 ++++++++++++ 3 files changed, 75 insertions(+), 26 deletions(-) diff --git a/packages/vee-validate/src/useField.ts b/packages/vee-validate/src/useField.ts index ead79c3b5..6868844d0 100644 --- a/packages/vee-validate/src/useField.ts +++ b/packages/vee-validate/src/useField.ts @@ -417,7 +417,7 @@ export function useFieldMeta(initialValue: MaybeRef, currentValu touched: false, pending: false, valid: true, - validated: false, + validated: !!unref(errors).length, initialValue: computed(() => unref(initialValue) as TValue | undefined), dirty: computed(() => { return !isEqual(unref(currentValue), unref(initialValue)); diff --git a/packages/vee-validate/src/useForm.ts b/packages/vee-validate/src/useForm.ts index 5d1b734d2..80db52bc2 100644 --- a/packages/vee-validate/src/useForm.ts +++ b/packages/vee-validate/src/useForm.ts @@ -103,6 +103,35 @@ export function useForm = Record); }); + /** + * Holds a computed reference to all fields names and labels + */ + const fieldNames = computed(() => { + return keysOf(fieldsById.value).reduce((names, path) => { + const field = normalizeField(fieldsById.value[path]); + if (field) { + names[path as string] = unref(field.label || field.name) || ''; + } + + return names; + }, {} as Record); + }); + + const fieldBailsMap = computed(() => { + return keysOf(fieldsById.value).reduce((map, path) => { + const field = normalizeField(fieldsById.value[path]); + if (field) { + map[path as string] = field.bails ?? true; + } + + return map; + }, {} as Record); + }); + + // mutable non-reactive reference to initial errors + // we need this to process initial errors then unset them + const initialErrors = { ...(opts?.initialErrors || {}) }; + // initial form values const { readonlyInitialValues, initialValues, setInitialValues } = useFormInitialValues( fieldsById, @@ -284,6 +313,18 @@ export function useForm = Record) { @@ -451,31 +492,6 @@ export function useForm = Record { - return keysOf(fieldsById.value).reduce((names, path) => { - const field = normalizeField(fieldsById.value[path]); - if (field) { - names[path as string] = unref(field.label || field.name) || ''; - } - - return names; - }, {} as Record); - }); - - const fieldBailsMap = computed(() => { - return keysOf(fieldsById.value).reduce((map, path) => { - const field = normalizeField(fieldsById.value[path]); - if (field) { - map[path as string] = field.bails ?? true; - } - - return map; - }, {} as Record); - }); - async function validateSchema(mode: SchemaValidationMode): Promise> { const schemaValue = unref(schema); if (!schemaValue) { diff --git a/packages/vee-validate/tests/Form.spec.ts b/packages/vee-validate/tests/Form.spec.ts index 7429cdcfd..1b6b6715d 100644 --- a/packages/vee-validate/tests/Form.spec.ts +++ b/packages/vee-validate/tests/Form.spec.ts @@ -2082,4 +2082,37 @@ describe('
', () => { await flushPromises(); expect(document.querySelectorAll('.error')).toHaveLength(2); }); + + // #3342 + test('field with pre-register errors should be checked on register', async () => { + const isShown = ref(false); + const modelValue = ref(''); + const wrapper = mountWithHoc({ + setup() { + return { + isShown, + modelValue, + schema: { + fname: 'required', + }, + }; + }, + template: ` + + + {{ errors.fname }} + + `, + }); + + await flushPromises(); + const span = wrapper.$el.querySelector('span'); + expect(span.textContent).toBe(REQUIRED_MESSAGE); + modelValue.value = 'hello'; + isShown.value = true; + + await flushPromises(); + // field was re-checked + expect(span.textContent).toBe(''); + }); });