@@ -22,6 +22,14 @@ function transformArrayToOptions(arr) {
22
22
} ) ) ;
23
23
}
24
24
25
+ // Function that handles validation object needed for each form component
26
+ function determineValidation ( fieldName , fieldObject , requiredArray ) {
27
+ return {
28
+ "required" : requiredArray . includes ( fieldName )
29
+ }
30
+ }
31
+
32
+ // Function that determines type of form component based on field
25
33
function determineType ( field ) {
26
34
if ( field . type === "object" ) {
27
35
return "container" ;
@@ -52,16 +60,18 @@ function determineType(field) {
52
60
}
53
61
54
62
// Creates Form.io component based on json field type
55
- function createComponent ( fieldName , fieldObject ) {
63
+ function createComponent ( fieldName , fieldObject , requiredArray ) {
56
64
const componentType = determineType ( fieldObject ) ;
65
+ const validate = determineValidation ( fieldName , fieldObject , requiredArray ) ;
57
66
switch ( componentType ) {
58
67
case "textfield" :
59
68
return {
60
69
type : "textfield" ,
61
70
key : fieldName ,
62
71
label : fieldName ,
63
72
input : true ,
64
- description : fieldObject [ "description" ]
73
+ description : fieldObject [ "description" ] ,
74
+ validate
65
75
} ;
66
76
case "tags" :
67
77
return {
@@ -72,7 +82,8 @@ function createComponent(fieldName, fieldObject) {
72
82
key : fieldName ,
73
83
type : "tags" ,
74
84
input : true ,
75
- description : fieldObject [ "description" ]
85
+ description : fieldObject [ "description" ] ,
86
+ validate
76
87
} ;
77
88
case "number" :
78
89
return {
@@ -88,7 +99,8 @@ function createComponent(fieldName, fieldObject) {
88
99
key : fieldName ,
89
100
type : "number" ,
90
101
input : true ,
91
- description : fieldObject [ "description" ]
102
+ description : fieldObject [ "description" ] ,
103
+ validate
92
104
} ;
93
105
case "radio" :
94
106
var options = transformArrayToOptions ( fieldObject . enum ) ;
@@ -104,6 +116,7 @@ function createComponent(fieldName, fieldObject) {
104
116
type : "radio" ,
105
117
input : true ,
106
118
description : fieldObject [ "description" ] ,
119
+ validate
107
120
} ;
108
121
case "selectboxes" :
109
122
var options = transformArrayToOptions ( fieldObject . items . enum ) ;
@@ -118,7 +131,8 @@ function createComponent(fieldName, fieldObject) {
118
131
type : "selectboxes" ,
119
132
input : true ,
120
133
inputType : "checkbox" ,
121
- description : fieldObject [ "description" ]
134
+ description : fieldObject [ "description" ] ,
135
+ validate
122
136
} ;
123
137
case "datetime" :
124
138
return {
@@ -152,7 +166,8 @@ function createComponent(fieldName, fieldObject) {
152
166
disableWeekdays : false ,
153
167
maxDate : null
154
168
} ,
155
- description : fieldObject [ "description" ]
169
+ description : fieldObject [ "description" ] ,
170
+ validate
156
171
} ;
157
172
case "select-boolean" :
158
173
return {
@@ -175,7 +190,8 @@ function createComponent(fieldName, fieldObject) {
175
190
key : fieldName ,
176
191
type : "select" ,
177
192
input : true ,
178
- description : fieldObject [ "description" ]
193
+ description : fieldObject [ "description" ] ,
194
+ validate
179
195
} ;
180
196
case "container" :
181
197
return {
@@ -185,7 +201,8 @@ function createComponent(fieldName, fieldObject) {
185
201
key : fieldName ,
186
202
type : "container" ,
187
203
input : true ,
188
- components : [ ]
204
+ components : [ ] ,
205
+ validate
189
206
} ;
190
207
case "datagrid" :
191
208
return {
@@ -203,7 +220,8 @@ function createComponent(fieldName, fieldObject) {
203
220
key : fieldName ,
204
221
type : "datagrid" ,
205
222
input : true ,
206
- components : [ ]
223
+ components : [ ] ,
224
+ validate
207
225
} ;
208
226
default :
209
227
break ;
@@ -222,14 +240,19 @@ function createAllComponents(schema, prefix = ""){
222
240
223
241
if ( schema . type === "object" && schema . properties ) {
224
242
225
- const items = schema . properties . hasOwnProperty ( "items" ) ? schema . properties . items : schema . properties
243
+ const items = schema . properties . hasOwnProperty ( "items" ) ? schema . properties . items : schema . properties ;
244
+
245
+ let requiredArray = [ ] ;
246
+ if ( schema . hasOwnProperty ( "required" ) ) {
247
+ requiredArray = schema . required ;
248
+ }
226
249
227
250
for ( const [ key , value ] of Object . entries ( items ) ) {
228
251
229
252
console . log ( "key at play:" , key ) ;
230
253
const fullKey = prefix ? `${ prefix } .${ key } ` : key ;
231
254
232
- let fieldComponent = createComponent ( key , value ) ;
255
+ let fieldComponent = createComponent ( key , value , requiredArray ) ;
233
256
234
257
if ( fieldComponent . type === "container" ) {
235
258
fieldComponent . components = createAllComponents ( value , fullKey ) ;
0 commit comments