@@ -18,11 +18,13 @@ import { addMethod, string } from 'yup'
18
18
import { useAppConfig } from '@/common-components/stores/UseAppConfig.js'
19
19
import { useDebounceFn } from '@vueuse/core'
20
20
import { useDescriptionValidatorService } from '@/common-components/validators/UseDescriptionValidatorService.js'
21
+ import { useCheckIfAnswerChangedForValidation } from '@/common-components/utilities/UseCheckIfAnswerChangedForValidation.js'
21
22
import { useLog } from '@/components/utils/misc/useLog.js'
22
23
23
24
export const useCustomGlobalValidators = ( ) => {
24
25
25
26
const descriptionValidatorService = useDescriptionValidatorService ( )
27
+ const checkIfAnswerChangedForValidation = useCheckIfAnswerChangedForValidation ( )
26
28
const log = useLog ( )
27
29
28
30
function customNameValidator ( fieldName = '' ) {
@@ -42,16 +44,21 @@ export const useCustomGlobalValidators = () => {
42
44
return this . test ( "customNameValidator" , null , ( value , context ) => validateName ( value , context ) ) ;
43
45
}
44
46
45
- function customDescriptionValidator ( fieldName = '' , enableProjectIdParam = true , useProtectedCommunityValidator = null , fieldNameFunction = null , enableQuizIdParam = true ) {
47
+ function customDescriptionValidator ( fieldName = '' , enableProjectIdParam = true , useProtectedCommunityValidator = null , fieldNameFunction = null , enableQuizIdParam = true , isSubmitting = false , errors = null ) {
46
48
const appConfig = useAppConfig ( )
47
- const validateName = useDebounceFn ( ( value , context ) => {
49
+ const validateDescription = useDebounceFn ( ( value , context ) => {
48
50
if ( ! value || value . trim ( ) . length === 0 || ! appConfig . paragraphValidationRegex ) {
49
51
return true
50
52
}
53
+ const forceAnswerValidation = isSubmitting || errors ?. hasOwnProperty ( context . path )
54
+ if ( ! forceAnswerValidation && ! checkIfAnswerChangedForValidation . hasValueChanged ( context . originalValue , context ) ) {
55
+ return true
56
+ }
51
57
return descriptionValidatorService . validateDescription ( value , enableProjectIdParam , useProtectedCommunityValidator , enableQuizIdParam ) . then ( ( result ) => {
52
58
if ( result . valid ) {
53
59
return true
54
60
}
61
+ checkIfAnswerChangedForValidation . removeAnswer ( context )
55
62
let fieldNameToUse = fieldName ? fieldName : '' ;
56
63
if ( ! fieldNameToUse && fieldNameFunction ) {
57
64
fieldNameToUse = fieldNameFunction ( context ) ;
@@ -63,7 +70,7 @@ export const useCustomGlobalValidators = () => {
63
70
} )
64
71
} , appConfig . formFieldDebounceInMs )
65
72
66
- return this . test ( "customDescriptionValidator" , null , ( value , context ) => validateName ( value , context ) ) ;
73
+ return this . test ( "customDescriptionValidator" , null , ( value , context ) => validateDescription ( value , context ) ) ;
67
74
}
68
75
69
76
function nullValueNotAllowed ( ) {
0 commit comments