From 20ac4b3332267491067f3a5a393ca97e3ec6bab5 Mon Sep 17 00:00:00 2001 From: Anthony Sendra Date: Sat, 27 Jan 2024 11:41:56 +0100 Subject: [PATCH] feat(Form): handle multiple paths in `validate` (#1273) --- docs/content/3.forms/10.form.md | 2 +- src/runtime/components/forms/Form.vue | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/docs/content/3.forms/10.form.md b/docs/content/3.forms/10.form.md index 121bd9f6..cb92d2c9 100644 --- a/docs/content/3.forms/10.form.md +++ b/docs/content/3.forms/10.form.md @@ -208,7 +208,7 @@ When accessing the component via a template ref, you can use the following: ::field{name="submit ()" type="Promise"} Triggers form submission. :: - ::field{name="validate (path?: string, opts: { silent?: boolean })" type="Promise"} + ::field{name="validate (path?: string | string[], opts: { silent?: boolean })" type="Promise"} Triggers form validation. Will raise any errors unless `opts.silent` is set to true. :: ::field{name="clear (path?: string)"} diff --git a/src/runtime/components/forms/Form.vue b/src/runtime/components/forms/Form.vue index 414a60a8..25b4df5f 100644 --- a/src/runtime/components/forms/Form.vue +++ b/src/runtime/components/forms/Form.vue @@ -89,13 +89,19 @@ export default defineComponent({ return errs } - async function validate (path?: string, opts: { silent?: boolean } = { silent: false }) { - if (path) { + async function validate (path?: string | string[], opts: { silent?: boolean } = { silent: false }) { + let paths = path + + if (path && !Array.isArray(path)) { + paths = [path] + } + + if (paths) { const otherErrors = errors.value.filter( - (error) => error.path !== path + (error) => !paths.includes(error.path) ) const pathErrors = (await getErrors()).filter( - (error) => error.path === path + (error) => paths.includes(error.path) ) errors.value = otherErrors.concat(pathErrors) } else {