@@ -22,6 +22,14 @@ function transformArrayToOptions(arr) {
2222 } ) ) ;
2323}
2424
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
2533function determineType ( field ) {
2634 if ( field . type === "object" ) {
2735 return "container" ;
@@ -52,16 +60,18 @@ function determineType(field) {
5260}
5361
5462// Creates Form.io component based on json field type
55- function createComponent ( fieldName , fieldObject ) {
63+ function createComponent ( fieldName , fieldObject , requiredArray ) {
5664 const componentType = determineType ( fieldObject ) ;
65+ const validate = determineValidation ( fieldName , fieldObject , requiredArray ) ;
5766 switch ( componentType ) {
5867 case "textfield" :
5968 return {
6069 type : "textfield" ,
6170 key : fieldName ,
6271 label : fieldName ,
6372 input : true ,
64- description : fieldObject [ "description" ]
73+ description : fieldObject [ "description" ] ,
74+ validate
6575 } ;
6676 case "tags" :
6777 return {
@@ -72,7 +82,8 @@ function createComponent(fieldName, fieldObject) {
7282 key : fieldName ,
7383 type : "tags" ,
7484 input : true ,
75- description : fieldObject [ "description" ]
85+ description : fieldObject [ "description" ] ,
86+ validate
7687 } ;
7788 case "number" :
7889 return {
@@ -88,7 +99,8 @@ function createComponent(fieldName, fieldObject) {
8899 key : fieldName ,
89100 type : "number" ,
90101 input : true ,
91- description : fieldObject [ "description" ]
102+ description : fieldObject [ "description" ] ,
103+ validate
92104 } ;
93105 case "radio" :
94106 var options = transformArrayToOptions ( fieldObject . enum ) ;
@@ -104,6 +116,7 @@ function createComponent(fieldName, fieldObject) {
104116 type : "radio" ,
105117 input : true ,
106118 description : fieldObject [ "description" ] ,
119+ validate
107120 } ;
108121 case "selectboxes" :
109122 var options = transformArrayToOptions ( fieldObject . items . enum ) ;
@@ -118,7 +131,8 @@ function createComponent(fieldName, fieldObject) {
118131 type : "selectboxes" ,
119132 input : true ,
120133 inputType : "checkbox" ,
121- description : fieldObject [ "description" ]
134+ description : fieldObject [ "description" ] ,
135+ validate
122136 } ;
123137 case "datetime" :
124138 return {
@@ -152,7 +166,8 @@ function createComponent(fieldName, fieldObject) {
152166 disableWeekdays : false ,
153167 maxDate : null
154168 } ,
155- description : fieldObject [ "description" ]
169+ description : fieldObject [ "description" ] ,
170+ validate
156171 } ;
157172 case "select-boolean" :
158173 return {
@@ -175,7 +190,8 @@ function createComponent(fieldName, fieldObject) {
175190 key : fieldName ,
176191 type : "select" ,
177192 input : true ,
178- description : fieldObject [ "description" ]
193+ description : fieldObject [ "description" ] ,
194+ validate
179195 } ;
180196 case "container" :
181197 return {
@@ -185,7 +201,8 @@ function createComponent(fieldName, fieldObject) {
185201 key : fieldName ,
186202 type : "container" ,
187203 input : true ,
188- components : [ ]
204+ components : [ ] ,
205+ validate
189206 } ;
190207 case "datagrid" :
191208 return {
@@ -203,7 +220,8 @@ function createComponent(fieldName, fieldObject) {
203220 key : fieldName ,
204221 type : "datagrid" ,
205222 input : true ,
206- components : [ ]
223+ components : [ ] ,
224+ validate
207225 } ;
208226 default :
209227 break ;
@@ -222,14 +240,19 @@ function createAllComponents(schema, prefix = ""){
222240
223241 if ( schema . type === "object" && schema . properties ) {
224242
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+ }
226249
227250 for ( const [ key , value ] of Object . entries ( items ) ) {
228251
229252 console . log ( "key at play:" , key ) ;
230253 const fullKey = prefix ? `${ prefix } .${ key } ` : key ;
231254
232- let fieldComponent = createComponent ( key , value ) ;
255+ let fieldComponent = createComponent ( key , value , requiredArray ) ;
233256
234257 if ( fieldComponent . type === "container" ) {
235258 fieldComponent . components = createAllComponents ( value , fullKey ) ;
0 commit comments