diff --git a/apps/showcase/doc/forms/ValidateOnDoc.vue b/apps/showcase/doc/forms/ValidateOnDoc.vue index e8d9a19ed..f48e52287 100644 --- a/apps/showcase/doc/forms/ValidateOnDoc.vue +++ b/apps/showcase/doc/forms/ValidateOnDoc.vue @@ -2,7 +2,7 @@

Form component supports flexible validation triggers, allowing validation on value updates, blur events, form mount, or submission, with options to apply this behavior globally or to specific fields via validateOnValueUpdate, - validateOnBlur, validateOnMount, and validateOnSubmit. + validateOnBlur, validateOnMount, and validateOnSubmit, which can also be set in PrimeVue components using formControl property.

@@ -12,7 +12,7 @@ {{ $form.username.error.message }}
- + {{ $form.name.error.message }}
@@ -42,7 +42,7 @@ export default { {{ $form.username.error.message }}
- + {{ $form.name.error.message }}
@@ -63,7 +63,7 @@ export default { {{ $form.username.error.message }}
- + {{ $form.name.error.message }}
@@ -126,7 +126,7 @@ export default { {{ $form.username.error.message }}
- + {{ $form.name.error.message }}
diff --git a/packages/form/src/useform/index.js b/packages/form/src/useform/index.js index 15879fc0b..8c17dff23 100644 --- a/packages/form/src/useform/index.js +++ b/packages/form/src/useform/index.js @@ -9,6 +9,7 @@ function tryOnMounted(fn, sync = true) { export const useForm = (options = {}) => { const states = reactive({}); + const fieldOptionMap = reactive({}); const valid = computed(() => Object.values(states).every((field) => !field.invalid)); const getInitialState = (field) => { @@ -32,6 +33,7 @@ export const useForm = (options = {}) => { const defineField = (field, fieldOptions) => { states[field] ||= getInitialState(field); + fieldOptionMap[field] = fieldOptions; const props = mergeProps(resolve(fieldOptions, states[field])?.props, resolve(fieldOptions?.props, states[field]), { name: field, @@ -63,7 +65,7 @@ export const useForm = (options = {}) => { states[field].dirty = true; } - (fieldOptions?.validateOnBlur ?? isFieldValidate(field, options.validateOnValueUpdate ?? true)) && validate(field); + (fieldOptions?.validateOnValueUpdate ?? isFieldValidate(field, options.validateOnValueUpdate ?? true)) && validate(field); } ); @@ -117,6 +119,9 @@ export const useForm = (options = {}) => { }; const validateOnMounted = () => { + const field = Object.keys(fieldOptionMap).find((field) => fieldOptionMap[field]?.validateOnMount); + + field && validate(field); isArray(options.validateOnMount) ? options.validateOnMount.forEach(validate) : validate(); };