Skip to content

Commit feffc53

Browse files
committed
PB-2064: fix report problem field validation
1 parent 0d666ae commit feffc53

File tree

3 files changed

+62
-80
lines changed

3 files changed

+62
-80
lines changed

packages/viewer/src/utils/components/EmailInput.vue

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
<script setup lang="ts">
2-
import { useTemplateRef } from 'vue'
2+
import { toRefs, useTemplateRef, withDefaults } from 'vue'
33
import { useI18n } from 'vue-i18n'
44
55
import { useComponentUniqueId } from '@/utils/composables/useComponentUniqueId'
66
import { useFieldValidation } from '@/utils/composables/useFieldValidation'
77
import { isValidEmail } from '@/utils/utils'
88
9-
const {
10-
label = '',
11-
description = '',
12-
disabled = false,
13-
placeholder = '',
14-
required = false,
15-
validMarker = undefined,
16-
validMessage = '',
17-
invalidMarker = undefined,
18-
invalidMessage = '',
19-
activateValidation = false,
20-
validate = undefined,
21-
dataCy = '',
22-
} = defineProps<{
9+
const props = withDefaults(defineProps<{
2310
/** Label to add above the field */
2411
label?: string
2512
/** Description to add below the input */
@@ -81,24 +68,25 @@ const {
8168
*/
8269
validate?: ((_value?: string) => { valid: boolean; invalidMessage: string }) | undefined
8370
dataCy?: string
84-
}>()
71+
}>(), {
72+
validMarker: undefined,
73+
invalidMarker: undefined,
74+
})
75+
76+
const {
77+
label,
78+
description,
79+
disabled,
80+
placeholder,
81+
dataCy,
82+
} = toRefs(props)
8583
8684
const inputEmailId = useComponentUniqueId('email-input')
8785
8886
const model = defineModel<string>({ default: '' })
8987
const emits = defineEmits(['change', 'validate', 'focusin', 'focusout', 'keydown.enter'])
9088
const { t } = useI18n()
9189
92-
const validationProps = {
93-
required,
94-
validMarker,
95-
validMessage,
96-
invalidMarker,
97-
invalidMessage,
98-
activateValidation,
99-
validate,
100-
}
101-
10290
const {
10391
value,
10492
validMarker: computedValidMarker,
@@ -108,7 +96,7 @@ const {
10896
required: computedRequired,
10997
onFocus,
11098
} = useFieldValidation(
111-
validationProps,
99+
props,
112100
model,
113101
emits as (_event: string, ..._args: unknown[]) => void,
114102
{

packages/viewer/src/utils/components/TextAreaInput.vue

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
<script setup lang="ts">
2-
import { useTemplateRef } from 'vue'
2+
import { toRefs, useTemplateRef, withDefaults } from 'vue'
33
import { useI18n } from 'vue-i18n'
44
55
import { useComponentUniqueId } from '@/utils/composables/useComponentUniqueId'
66
import { useFieldValidation } from '@/utils/composables/useFieldValidation'
77
8-
const {
9-
label = '',
10-
description = '',
11-
disabled = false,
12-
placeholder = '',
13-
required = false,
14-
validMarker = undefined,
15-
validMessage = '',
16-
invalidMarker = undefined,
17-
invalidMessage = '',
18-
activateValidation = false,
19-
validate = undefined,
20-
dataCy = '',
21-
} = defineProps<{
8+
const props = withDefaults(defineProps<{
229
/** Label to add above the field */
2310
label?: string
2411
/** Description to add below the input */
@@ -80,7 +67,18 @@ const {
8067
*/
8168
validate?: ((_value?: string) => { valid: boolean; invalidMessage: string }) | undefined
8269
dataCy?: string
83-
}>()
70+
}>(), {
71+
validMarker: undefined,
72+
invalidMarker: undefined,
73+
})
74+
75+
const {
76+
label,
77+
description,
78+
disabled,
79+
placeholder,
80+
dataCy,
81+
} = toRefs(props)
8482
8583
const textAreaInputId = useComponentUniqueId('text-area-input')
8684
@@ -90,16 +88,6 @@ const { t } = useI18n()
9088
9189
const textAreaElement = useTemplateRef('textAreaElement')
9290
93-
const validationProps = {
94-
required,
95-
validMarker,
96-
validMessage,
97-
invalidMarker,
98-
invalidMessage,
99-
activateValidation,
100-
validate,
101-
}
102-
10391
const {
10492
value,
10593
validMarker: computedValidMarker,
@@ -109,7 +97,7 @@ const {
10997
onFocus,
11098
required: computedRequired,
11199
} = useFieldValidation(
112-
validationProps,
100+
props,
113101
model,
114102
emits as (_event: string, ..._args: unknown[]) => void
115103
)

packages/viewer/src/utils/composables/useFieldValidation.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 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

55
export 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

Comments
 (0)