|
8 | 8 |
|
9 | 9 | import React from 'react'; |
10 | 10 | import axios from '@mapstore/framework/libs/ajax'; |
11 | | -import isArray from 'lodash/isArray'; |
| 11 | +import castArray from 'lodash/castArray'; |
12 | 12 | import isEmpty from 'lodash/isEmpty'; |
13 | 13 | import isString from 'lodash/isString'; |
14 | | -import get from 'lodash/get'; |
15 | 14 | import Autocomplete from '@js/components/Autocomplete/Autocomplete'; |
16 | 15 | import DefaultSchemaField from '@rjsf/core/lib/components/fields/SchemaField'; |
17 | 16 |
|
@@ -41,9 +40,19 @@ const SchemaField = (props) => { |
41 | 40 | const isSingleSelect = schema?.type === 'object' && !isEmpty(schema?.properties); |
42 | 41 |
|
43 | 42 | if (autocomplete && (isMultiSelect || isSingleSelect)) { |
44 | | - const errors = !hideError ? isArray(errorSchema) |
45 | | - ? errorSchema.map(error => get(error, 'id.__errors', [])).flat() |
46 | | - : get(errorSchema, '__errors', []) : []; |
| 43 | + const errors = (!hideError ? castArray(errorSchema) : []) |
| 44 | + .reduce((acc, errorEntry) => { |
| 45 | + if (errorEntry?.__errors) { |
| 46 | + acc.push({ messages: errorEntry.__errors }); |
| 47 | + } else { |
| 48 | + Object.keys(errorEntry || {}).forEach((key) => { |
| 49 | + if (errorEntry[key]?.__errors) { |
| 50 | + acc.push({ key, messages: errorEntry[key].__errors }); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + return acc; |
| 55 | + }, []); |
47 | 56 | const autocompleteOptions = isString(autocomplete) |
48 | 57 | ? { url: autocomplete } |
49 | 58 | : autocomplete; |
@@ -112,15 +121,17 @@ const SchemaField = (props) => { |
112 | 121 | }; |
113 | 122 | }); |
114 | 123 | }, |
115 | | - error: isEmpty(errors) ? null : <ul> |
116 | | - {errors.map((error, idx) => { |
| 124 | + error: isEmpty(errors) ? null : <> |
| 125 | + {errors.map((entry, idx) => { |
117 | 126 | return ( |
118 | | - <li key={idx} className="gn-error-message"> |
119 | | - {error} |
120 | | - </li> |
| 127 | + <ul key={idx} className="text-danger"> |
| 128 | + {castArray(entry.messages).map((message, mIdx) => { |
| 129 | + return <li key={mIdx}>{entry.key ? `${entry.key}: ` : ''}{message}</li>; |
| 130 | + })} |
| 131 | + </ul> |
121 | 132 | ); |
122 | 133 | })} |
123 | | - </ul> |
| 134 | + </> |
124 | 135 | }; |
125 | 136 |
|
126 | 137 | return <Autocomplete {...autoCompleteProps}/>; |
|
0 commit comments