File tree 3 files changed +52
-1
lines changed
3 files changed +52
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' vee-validate ' : patch
3
+ ---
4
+
5
+ fixed validations running for unmounted fields
Original file line number Diff line number Diff line change @@ -407,11 +407,13 @@ function _useField<TValue = unknown>(
407
407
onBeforeUnmount ( ( ) => {
408
408
PENDING_UNMOUNT = true ;
409
409
const shouldKeepValue = unref ( field . keepValueOnUnmount ) ?? unref ( form . keepValuesOnUnmount ) ;
410
+ const path = unravel ( name ) ;
410
411
if ( shouldKeepValue || ! form ) {
412
+ form ?. removePathState ( path ) ;
413
+
411
414
return ;
412
415
}
413
416
414
- const path = unravel ( name ) ;
415
417
const pathState = form . getPathState ( path ) ;
416
418
const matchesId =
417
419
Array . isArray ( pathState ?. id ) && pathState ?. multiple
Original file line number Diff line number Diff line change @@ -3043,3 +3043,47 @@ test('unmounted radio fields gets unregistered and their values are removed if c
3043
3043
expect ( errors . textContent ) . toBe ( '{}' ) ;
3044
3044
expect ( JSON . parse ( values . textContent ) ) . toEqual ( { } ) ;
3045
3045
} ) ;
3046
+
3047
+ // #4247
3048
+ test ( 'unmounted fields should not be validated when keep-values is on' , async ( ) => {
3049
+ let showFields ! : Ref < boolean > ;
3050
+ const spy = vi . fn ( ) ;
3051
+ const wrapper = mountWithHoc ( {
3052
+ setup ( ) {
3053
+ showFields = ref ( true ) ;
3054
+
3055
+ return {
3056
+ showFields,
3057
+ onSubmit ( values : any ) {
3058
+ spy ( values ) ;
3059
+ } ,
3060
+ } ;
3061
+ } ,
3062
+ template : `
3063
+ <VForm @submit="onSubmit" v-slot="{ errors }" keep-values>
3064
+ <template v-if="showFields">
3065
+ <Field name="test" rules="required" />
3066
+ </template>
3067
+
3068
+ <button>Submit</button>
3069
+ </VForm>
3070
+ ` ,
3071
+ } ) ;
3072
+
3073
+ await flushPromises ( ) ;
3074
+ const button = wrapper . $el . querySelector ( 'button' ) ;
3075
+ const inputs = wrapper . $el . querySelectorAll ( 'input' ) ;
3076
+
3077
+ wrapper . $el . querySelector ( 'button' ) . click ( ) ;
3078
+ await flushPromises ( ) ;
3079
+ setValue ( inputs [ 0 ] , '' ) ;
3080
+
3081
+ showFields . value = false ;
3082
+ await flushPromises ( ) ;
3083
+ button . click ( ) ;
3084
+ await flushPromises ( ) ;
3085
+ const expected = {
3086
+ test : '' ,
3087
+ } ;
3088
+ expect ( spy ) . toHaveBeenLastCalledWith ( expected ) ;
3089
+ } ) ;
You can’t perform that action at this time.
0 commit comments