dany giguere
03/10/2023, 1:20 AM{
"description": [
"The field Description must be no more than 25 characters long",
"The field Description must be at least 6 characters long"
],
"title": [
"The field Title must be between 6 and 25 characters long"
]
}
This is my code :
val errorMap: MutableMap<String, ArrayList<String>> = HashMap()
val stringArray: ArrayList<String> = ArrayList()
e.bindingResult.fieldErrors.forEach(Consumer { error: FieldError ->
error.defaultMessage?.let { stringArray.add(it) }
errorMap[error.field] = stringArray
})
But it returns:
{
"description": [
"The field Title must be between 6 and 25 characters long",
"The field Description must be no more than 25 characters long",
"The field Description must be at least 6 characters long"
],
"title": [
"The field Title must be between 6 and 25 characters long",
"The field Description must be no more than 25 characters long",
"The field Description must be at least 6 characters long"
]
}
What’s the most elegant to to that with Kotlin ?Svyatoslav Kuzmich [JB]
03/10/2023, 6:39 AMstringArray
.
I like to do it in defaultValue
of getOrPut
method:
val message = error.defaultMessage
if (message != null) {
errorMap.getOrPut(error.field, defaultValue = { ArrayList() }).add(message)
}
Joffrey
03/10/2023, 8:28 AMDavid Kubecka
03/10/2023, 10:01 AMbuildMap<String, ArrayList<String>> {
e.bindingResult.fieldErrors.forEach { error ->
val message = error.defaultMessage
if (message != null) {
errorMap.getOrPut(error.field, defaultValue = { ArrayList() }).add(message)
}
}
}
David Kubecka
03/10/2023, 10:04 AMJoffrey
03/10/2023, 12:44 PMstringArray
is already a list in the original code despite its name. `ArrayList`s are listsDavid Kubecka
03/10/2023, 12:45 PMmutableListOf
part)dany giguere
03/10/2023, 2:33 PM