@@ -24,37 +24,54 @@ const INPUT_TYPES = {
24
24
STRING : ' STRING' ,
25
25
};
26
26
27
+ const VALIDATION_MESSAGES = {
28
+ ARRAY_FORMAT_MISMATCH : __ (
29
+ ' The value must be a valid JSON array format: [1,2,3] or [{"key": "value"}]' ,
30
+ ),
31
+ GENERAL_FORMAT_MISMATCH : __ (' Please match the requested format.' ),
32
+ NUMBER_TYPE_MISMATCH : __ (' The value must contain only numbers.' ),
33
+ REGEX_MISMATCH : __ (' The value must match the defined regular expression.' ),
34
+ VALUE_MISSING : __ (' This is required and must be defined.' ),
35
+ };
36
+
27
37
const feedbackMap = {
28
38
arrayFormatMismatch: {
29
39
isInvalid : (el ) => {
30
40
if (el .dataset .jsonArray !== ' true' || ! el .value ) return false ;
31
41
32
42
try {
33
- const parsed = JSON .parse (el .value );
34
- return ! Array .isArray (parsed);
43
+ const isValid = Array .isArray (JSON .parse (el .value ));
44
+ // we use setCustomValidity to set the message that appears when the user clicks submit
45
+ el .setCustomValidity (isValid ? ' ' : VALIDATION_MESSAGES .GENERAL_FORMAT_MISMATCH );
46
+ return ! isValid;
35
47
} catch {
48
+ el .setCustomValidity (VALIDATION_MESSAGES .GENERAL_FORMAT_MISMATCH );
36
49
return true ;
37
50
}
38
51
},
39
52
message: __ (' The value must be a valid JSON array format: [1,2,3] or [{"key": "value"}]' ),
40
53
},
41
54
numberTypeMismatch: {
42
55
isInvalid : (el ) => {
43
- return (
56
+ const isInvalid =
44
57
el .dataset .fieldType === INPUT_TYPES .NUMBER &&
45
58
el .value &&
46
- ! Number .isFinite (Number (el .value ))
47
- );
59
+ ! Number .isFinite (Number (el .value ));
60
+
61
+ // we use setCustomValidity to set the message that appears when the user clicks submit
62
+ el .setCustomValidity (isInvalid ? VALIDATION_MESSAGES .GENERAL_FORMAT_MISMATCH : ' ' );
63
+
64
+ return isInvalid;
48
65
},
49
- message: __ ( ' The value must contain only numbers. ' ) ,
66
+ message: VALIDATION_MESSAGES . NUMBER_TYPE_MISMATCH ,
50
67
},
51
68
regexMismatch: {
52
69
isInvalid : (el ) => el .validity ? .patternMismatch ,
53
- message: __ ( ' The value must match the defined regular expression. ' ) ,
70
+ message: VALIDATION_MESSAGES . REGEX_MISMATCH ,
54
71
},
55
72
valueMissing: {
56
73
isInvalid : (el ) => el .validity ? .valueMissing ,
57
- message: __ ( ' This is required and must be defined. ' ) ,
74
+ message: VALIDATION_MESSAGES . VALUE_MISSING ,
58
75
},
59
76
};
60
77
@@ -116,6 +133,13 @@ export default {
116
133
const field = this .form .fields [this .item .name ];
117
134
return this .isArrayType && field? .feedback === feedbackMap .arrayFormatMismatch .message ;
118
135
},
136
+ hasNumberTypeError () {
137
+ const field = this .form .fields [this .item .name ];
138
+ return (
139
+ this .item .type === INPUT_TYPES .NUMBER &&
140
+ field? .feedback === feedbackMap .numberTypeMismatch .message
141
+ );
142
+ },
119
143
hasValidationFeedback () {
120
144
return Boolean (this .validationFeedback );
121
145
},
@@ -137,9 +161,9 @@ export default {
137
161
: feedback;
138
162
},
139
163
validationState () {
140
- // Override validation state for array format errors
141
- // This handles cases where checkValidity() returns true but our custom array validation fails
142
- if (this .hasArrayFormatError ) {
164
+ // Override validation state for array format errors and number type errors for our custom validation
165
+ // This is also responsible for turning the border red when the input is invalid
166
+ if (this .hasArrayFormatError || this . hasNumberTypeError ) {
143
167
return false ;
144
168
}
145
169
0 commit comments