From 5cc92b2ed33ced1808eac14e6eb0b025899a0ffb Mon Sep 17 00:00:00 2001 From: John Leider Date: Mon, 10 Feb 2025 16:03:07 -0600 Subject: [PATCH] fix(validation): add validation watcher for rules fixes #18875 --- .../src/composables/__tests__/validation.spec.ts | 15 +++++++++++++++ packages/vuetify/src/composables/validation.ts | 4 +++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/vuetify/src/composables/__tests__/validation.spec.ts b/packages/vuetify/src/composables/__tests__/validation.spec.ts index 82838f56c56..31b438b8f92 100644 --- a/packages/vuetify/src/composables/__tests__/validation.spec.ts +++ b/packages/vuetify/src/composables/__tests__/validation.spec.ts @@ -173,4 +173,19 @@ describe('validation', () => { expect(wrapper.vm.isValid).toBe(true) }) + + it.only('should rerun validation when rules changes', async () => { + const wrapper = mountFunction({ + modelValue: 'bar', + rules: [(v: any) => v === 'foo' || 'Invalid'], + }) + + await wrapper.vm.validate() + + expect(wrapper.vm.isValid).toBe(false) + + await wrapper.setProps({ rules: [(v: any) => v === 'bar' || 'Invalid'] }) + + expect(wrapper.vm.isValid).toBe(true) + }) }) diff --git a/packages/vuetify/src/composables/validation.ts b/packages/vuetify/src/composables/validation.ts index 01a1868e4fa..d3a74680c50 100644 --- a/packages/vuetify/src/composables/validation.ts +++ b/packages/vuetify/src/composables/validation.ts @@ -6,7 +6,7 @@ import { useToggleScope } from '@/composables/toggleScope' // Utilities import { computed, nextTick, onBeforeMount, onBeforeUnmount, onMounted, ref, shallowRef, unref, watch } from 'vue' -import { getCurrentInstance, getCurrentInstanceName, getUid, propsFactory, wrapInArray } from '@/util' +import { deepEqual, getCurrentInstance, getCurrentInstanceName, getUid, propsFactory, wrapInArray } from '@/util' // Types import type { PropType } from 'vue' @@ -176,6 +176,8 @@ export function useValidation ( form.update?.(uid.value, isValid.value, errorMessages.value) }) + watch(() => props.rules, () => validate()) + async function reset () { model.value = null await nextTick()