11import type { ComputedRef , Ref } from 'vue'
22
3- import { computed , onMounted , ref , toRef , watch } from 'vue'
3+ import { computed , onMounted , reactive , ref , toRef , watch } from 'vue'
44
55export interface ValidationResult {
66 valid : boolean
@@ -92,7 +92,7 @@ export function useFieldValidation<T extends string | File>(
9292 const activateValidation = toRef ( props , 'activateValidation' )
9393 const required = toRef ( props , 'required' )
9494 const validMessage = toRef ( props , 'validMessage' )
95- const validation = ref < ValidationResult > ( { valid : true , invalidMessage : '' } )
95+ const validation = reactive < ValidationResult > ( { valid : true , invalidMessage : '' } )
9696
9797 // Helper function to check if value is empty
9898 const isEmpty = ( value ?: T ) : boolean => {
@@ -107,36 +107,37 @@ export function useFieldValidation<T extends string | File>(
107107
108108 // Computed properties
109109 const isValid = computed ( ( ) => {
110- return validation . value . valid
111- } )
112- const activateValidationMarkers = computed ( ( ) => activateValidation . value )
113- const validMarker = computed ( ( ) => {
114- // Do not add a valid marker when the marker are not activated or when the field is empty
115- // or when it is already marked as invalid
116- if ( ! activateValidationMarkers . value || isEmpty ( value . value ) || invalidMarker . value ) {
117- return false
118- }
119- let _validMarker = isValid . value
120- if ( props . validMarker !== undefined ) {
121- _validMarker = _validMarker && ( props . validMarker ?? false )
122- }
123- return _validMarker
110+ return validation . valid
124111 } )
125112 const invalidMarker = computed ( ( ) => {
126- if ( ! activateValidationMarkers . value ) {
113+ if ( ! activateValidation . value ) {
127114 return false
128115 }
129116 let _invalidMarker = ! isValid . value
130117 if ( props . invalidMarker !== undefined ) {
131118 _invalidMarker = _invalidMarker || ! ! props . invalidMarker
132119 }
120+ console . log ( 'Computed invalid marker:' , _invalidMarker )
133121 return _invalidMarker
134122 } )
123+ const validMarker = computed ( ( ) => {
124+ // Do not add a valid marker when the marker are not activated or when the field is empty
125+ // or when it is already marked as invalid
126+ if ( ! activateValidation . value || isEmpty ( value . value ) || ! isValid . value ) {
127+ return false
128+ }
129+ // If validMarker prop is explicitly provided (not undefined), use it
130+ if ( props . validMarker !== undefined ) {
131+ return props . validMarker
132+ }
133+ // Default: show valid marker when field is valid
134+ return true
135+ } )
135136 const invalidMessage = computed ( ( ) => {
136137 if ( props . invalidMessage ) {
137138 return props . invalidMessage
138139 }
139- return validation . value . invalidMessage
140+ return validation . invalidMessage
140141 } )
141142
142143 // Watches
@@ -154,18 +155,23 @@ export function useFieldValidation<T extends string | File>(
154155 } )
155156
156157 function validate ( ) : void {
157- validation . value = customValidate ( )
158+ const customResult = customValidate ( )
159+
160+ validation . valid = customResult . valid
161+ validation . invalidMessage = customResult . invalidMessage
162+
158163 if ( required . value && isEmpty ( value . value ) ) {
159- validation . value = {
160- valid : false ,
161- invalidMessage : requiredInvalidMessage ,
162- }
164+ validation . valid = false
165+ validation . invalidMessage = requiredInvalidMessage
163166 }
164- if ( props . validate && validation . value . valid ) {
167+ if ( props . validate && validation . valid ) {
165168 // Run user custom validation
166- validation . value = props . validate ( value . value )
169+ const validateResult = props . validate ( value . value )
170+ validation . valid = validateResult . valid
171+ validation . invalidMessage = validateResult . invalidMessage
167172 }
168- emits ( 'validate' , validation . value . valid )
173+
174+ emits ( 'validate' , validation . valid )
169175 }
170176
171177 function onFocus ( event : Event , inFocus : boolean ) : void {
0 commit comments