@@ -1218,7 +1218,10 @@ define([
1218
1218
}
1219
1219
} ,
1220
1220
1221
- /** Show any errors that occured when trying to save changes */
1221
+ /**
1222
+ * Show any errors that occurred when trying to save changes
1223
+ *
1224
+ */
1222
1225
showValidation ( ) {
1223
1226
// First clear all the error messaging
1224
1227
this . $ ( ".notification.error" ) . empty ( ) ;
@@ -1228,75 +1231,101 @@ define([
1228
1231
1229
1232
const errors = this . model . validationError ;
1230
1233
1231
- errors ?. forEach ( ( errorMsg , category ) => {
1232
- const categoryEls = this . $ ( `[data-category='${ category } ']` ) ;
1233
- const dataItemRow = categoryEls . parents ( ".data-package-item" ) ;
1234
+ if ( errors && typeof errors === "object" ) {
1235
+ Object . entries ( errors ) . forEach ( ( [ category , errorMsg ] ) => {
1236
+ if ( typeof errorMsg === "string" ) {
1237
+ // Handle string error messages
1238
+ this . showError ( category , errorMsg ) ;
1239
+ } else if ( typeof errorMsg === "object" ) {
1240
+ // Handle object error messages by iterating over leaf nodes
1241
+ this . showLeafErrors ( category , errorMsg ) ;
1242
+ }
1243
+ } ) ;
1234
1244
1235
- // If this field is in a DataItemView, then delegate to that view
1236
- if ( dataItemRow . length && dataItemRow . data ( "view" ) ) {
1237
- dataItemRow . data ( "view" ) . showValidation ( category , errorMsg ) ;
1238
- return ;
1239
- }
1240
- const elsWithViews = categoryEls . filter (
1241
- ( el ) =>
1242
- $ ( el ) . data ( "view" ) &&
1243
- $ ( el ) . data ( "view" ) . showValidation &&
1244
- ! $ ( el ) . data ( "view" ) . isNew ,
1245
- ) ;
1245
+ if ( Object . keys ( errors ) . length ) {
1246
+ // Create a list of errors to display in the error message shown to the user
1247
+ const errorList = `<ul>${ this . getErrorListItem ( errors ) } </ul>` ;
1246
1248
1247
- if ( elsWithViews . length ) {
1248
- elsWithViews . forEach ( ( el ) => {
1249
- $ ( el ) . data ( "view" ) . showValidation ( ) ;
1250
- } ) ;
1251
- } else if ( categoryEls . length ) {
1252
- // Show the error message
1253
- categoryEls
1254
- . filter ( ".notification" )
1255
- . addClass ( "error" )
1256
- . text ( errorMsg ) ;
1257
-
1258
- // Add the error message to inputs
1259
- categoryEls . filter ( "textarea, input" ) . addClass ( "error" ) ;
1249
+ MetacatUI . appView . showAlert (
1250
+ `Fix the errors flagged below before submitting: ${ errorList } ` ,
1251
+ "alert-error" ,
1252
+ this . $el ,
1253
+ null ,
1254
+ {
1255
+ remove : true ,
1256
+ } ,
1257
+ ) ;
1260
1258
}
1259
+ }
1260
+ } ,
1261
1261
1262
- // Get the link in the table of contents navigation
1263
- let navigationLink = this . $ (
1264
- `.side-nav-item[data-category='${ category } ']` ,
1265
- ) ;
1262
+ /**
1263
+ * Log an error message for a specific category
1264
+ *
1265
+ * @param category - The category of the error
1266
+ * @param errorMsg - The error message to display
1267
+ */
1268
+ showError ( category , errorMsg ) {
1269
+ const categoryEls = this . $ ( `[data-category='${ category } ']` ) ;
1270
+ const dataItemRow = categoryEls . parents ( ".data-package-item" ) ;
1266
1271
1267
- if ( ! navigationLink . length ) {
1268
- const section = categoryEls . parents ( "[data-section]" ) ;
1269
- navigationLink = this . $ (
1270
- `.side-nav-item.${ $ ( section ) . attr ( "data-section" ) } ` ,
1271
- ) ;
1272
- }
1272
+ // If this field is in a DataItemView, then delegate to that view
1273
+ if ( dataItemRow . length && dataItemRow . data ( "view" ) ) {
1274
+ dataItemRow . data ( "view" ) . showValidation ( category , errorMsg ) ;
1275
+ return ;
1276
+ }
1277
+ const elsWithViews = categoryEls . filter (
1278
+ ( el ) =>
1279
+ $ ( el ) . data ( "view" ) &&
1280
+ $ ( el ) . data ( "view" ) . showValidation &&
1281
+ ! $ ( el ) . data ( "view" ) . isNew ,
1282
+ ) ;
1273
1283
1274
- // Show the error icon in the table of contents
1275
- navigationLink
1276
- . addClass ( "error" )
1277
- . find ( ".icon" )
1278
- . addClass ( "error" )
1279
- . show ( ) ;
1280
-
1281
- this . model . off ( `change:${ category } ` , this . model . checkValidity ) ;
1282
- this . model . once ( `change:${ category } ` , this . model . checkValidity ) ;
1283
- } , this ) ;
1284
-
1285
- if ( errors ) {
1286
- // Create a list of errors to display in the error message shown to
1287
- // the user
1288
- const errorList = `<ul>${ this . getErrorListItem ( errors ) } </ul>` ;
1289
-
1290
- MetacatUI . appView . showAlert (
1291
- `Fix the errors flagged below before submitting: ${ errorList } ` ,
1292
- "alert-error" ,
1293
- this . $el ,
1294
- null ,
1295
- {
1296
- remove : true ,
1297
- } ,
1284
+ if ( elsWithViews . length ) {
1285
+ elsWithViews . forEach ( ( el ) => {
1286
+ $ ( el ) . data ( "view" ) . showValidation ( ) ;
1287
+ } ) ;
1288
+ } else if ( categoryEls . length ) {
1289
+ // Show the error message
1290
+ categoryEls . filter ( ".notification" ) . addClass ( "error" ) . text ( errorMsg ) ;
1291
+
1292
+ // Add the error message to inputs
1293
+ categoryEls . filter ( "textarea, input" ) . addClass ( "error" ) ;
1294
+ }
1295
+
1296
+ // Get the link in the table of contents navigation
1297
+ let navigationLink = this . $ (
1298
+ `.side-nav-item[data-category='${ category } ']` ,
1299
+ ) ;
1300
+
1301
+ if ( ! navigationLink . length ) {
1302
+ const section = categoryEls . parents ( "[data-section]" ) ;
1303
+ navigationLink = this . $ (
1304
+ `.side-nav-item.${ $ ( section ) . attr ( "data-section" ) } ` ,
1298
1305
) ;
1299
1306
}
1307
+
1308
+ // Show the error icon in the table of contents
1309
+ navigationLink . addClass ( "error" ) . find ( ".icon" ) . addClass ( "error" ) . show ( ) ;
1310
+
1311
+ this . model . off ( `change:${ category } ` , this . model . checkValidity ) ;
1312
+ this . model . once ( `change:${ category } ` , this . model . checkValidity ) ;
1313
+ } ,
1314
+
1315
+ /**
1316
+ * Recursively log the leaf errors in the error object
1317
+ *
1318
+ * @param category - The category of the error
1319
+ * @param errorObj - The object containing the error messages
1320
+ */
1321
+ showLeafErrors ( category , errorObj ) {
1322
+ Object . entries ( errorObj ) . forEach ( ( [ subCategory , subErrorMsg ] ) => {
1323
+ if ( typeof subErrorMsg === "string" ) {
1324
+ this . showError ( `${ category } ` , subErrorMsg ) ;
1325
+ } else if ( typeof subErrorMsg === "object" ) {
1326
+ this . showLeafErrors ( `${ subCategory } ` , subErrorMsg ) ;
1327
+ }
1328
+ } ) ;
1300
1329
} ,
1301
1330
1302
1331
/** @inheritdoc */
0 commit comments