@@ -5,14 +5,17 @@ import { isFormValue } from '~/src/server/plugins/engine/components/FormComponen
55import { SelectionControlField } from '~/src/server/plugins/engine/components/SelectionControlField.js'
66import { type FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
77import { type QuestionPageController } from '~/src/server/plugins/engine/pageControllers/QuestionPageController.js'
8+ import { messageTemplate } from '~/src/server/plugins/engine/pageControllers/validationOptions.js'
89import {
10+ type ErrorMessageTemplateList ,
911 type FormState ,
1012 type FormStateValue ,
1113 type FormSubmissionState
1214} from '~/src/server/plugins/engine/types.js'
1315
1416export class CheckboxesField extends SelectionControlField {
1517 declare options : CheckboxesFieldComponent [ 'options' ]
18+ declare schema : CheckboxesFieldComponent [ 'schema' ]
1619 declare formSchema : ArraySchema < string > | ArraySchema < number >
1720 declare stateSchema : ArraySchema < string > | ArraySchema < number >
1821
@@ -24,6 +27,7 @@ export class CheckboxesField extends SelectionControlField {
2427
2528 const { listType : type } = this
2629 const { options } = def
30+ const schema = 'schema' in def ? def . schema : { }
2731
2832 let formSchema =
2933 type === 'string' ? joi . array < string > ( ) : joi . array < number > ( )
@@ -42,6 +46,18 @@ export class CheckboxesField extends SelectionControlField {
4246 formSchema = formSchema . optional ( )
4347 }
4448
49+ if ( typeof schema ?. length === 'number' ) {
50+ formSchema = formSchema . length ( schema . length )
51+ } else {
52+ if ( typeof schema ?. min === 'number' ) {
53+ formSchema = formSchema . min ( schema . min )
54+ }
55+
56+ if ( typeof schema ?. max === 'number' ) {
57+ formSchema = formSchema . max ( schema . max )
58+ }
59+ }
60+
4561 this . formSchema = formSchema . default ( [ ] )
4662 this . stateSchema = formSchema . default ( null ) . allow ( null )
4763 this . options = options
@@ -112,6 +128,30 @@ export class CheckboxesField extends SelectionControlField {
112128 return this . getContextValueFromFormValue ( values )
113129 }
114130
131+ /**
132+ * For error preview page that shows all possible errors on a component
133+ */
134+ getAllPossibleErrors ( ) : ErrorMessageTemplateList {
135+ return CheckboxesField . getAllPossibleErrors ( )
136+ }
137+
138+ /**
139+ * Static version of getAllPossibleErrors that doesn't require a component instance.
140+ */
141+ static getAllPossibleErrors ( ) : ErrorMessageTemplateList {
142+ const parentErrors = SelectionControlField . getAllPossibleErrors ( )
143+
144+ return {
145+ ...parentErrors ,
146+ advancedSettingsErrors : [
147+ ...parentErrors . advancedSettingsErrors ,
148+ { type : 'array.min' , template : messageTemplate . arrayMin } ,
149+ { type : 'array.max' , template : messageTemplate . arrayMax } ,
150+ { type : 'array.length' , template : messageTemplate . arrayLength }
151+ ]
152+ }
153+ }
154+
115155 isValue ( value ?: FormStateValue | FormState ) : value is Item [ 'value' ] [ ] {
116156 if ( ! Array . isArray ( value ) ) {
117157 return false
0 commit comments