feat(Form): handle multiple paths in validate (#1273)

This commit is contained in:
Anthony Sendra
2024-01-27 11:41:56 +01:00
committed by GitHub
parent 2e1ef557b5
commit 20ac4b3332
2 changed files with 11 additions and 5 deletions

View File

@@ -208,7 +208,7 @@ When accessing the component via a template ref, you can use the following:
::field{name="submit ()" type="Promise<void>"}
Triggers form submission.
::
::field{name="validate (path?: string, opts: { silent?: boolean })" type="Promise<T>"}
::field{name="validate (path?: string | string[], opts: { silent?: boolean })" type="Promise<T>"}
Triggers form validation. Will raise any errors unless `opts.silent` is set to true.
::
::field{name="clear (path?: string)"}

View File

@@ -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 {