Skip to content

Commit

Permalink
improved processSubmitErrors to render errors for every children in e…
Browse files Browse the repository at this point in the history
…rrors tree
  • Loading branch information
grimpows authored and nacmartin committed Dec 1, 2017
1 parent b8cbc99 commit f523f33
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/processSubmitErrors.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import { SubmissionError } from 'redux-form'
import _ from 'lodash' // added for empty check

const processSubmitErrors = response => {
let errors = {}
if (response.hasOwnProperty('errors')) {
for (let field in response.errors.children) {
let value = response.errors.children[field]
if (value.hasOwnProperty('errors')) {
errors[field] = value.errors[0]
const convertToReduxFormErrors = (obj) => {

let objectWithoutChildrenAndFalseErrors = {}
Object.keys(obj).map( (name) => {
if(name === 'children'){
objectWithoutChildrenAndFalseErrors = { ...objectWithoutChildrenAndFalseErrors, ...convertToReduxFormErrors(obj[name])}
}else{
if( obj[name].hasOwnProperty('children')){ // if children, take field from it and set them directly as own field
objectWithoutChildrenAndFalseErrors[name] = convertToReduxFormErrors(obj[name])
}else{
if( obj[name].hasOwnProperty('errors') && !_.isEmpty(obj[name]['errors']) ){ // using lodash for empty error check, dont add them if empty
objectWithoutChildrenAndFalseErrors[name] = obj[name]['errors']
}
}
}
}
throw new SubmissionError(errors)
}
)
return objectWithoutChildrenAndFalseErrors
}

const processSubmitErrors = (errors) => {
if(errors.hasOwnProperty('errors')){
errors = convertToReduxFormErrors(errors.errors)
throw new SubmissionError(errors)
}
return {}
return {} // dont know why original processSubmitErrors return empty object, keep this to prevent any BC break
}

export default processSubmitErrors

0 comments on commit f523f33

Please sign in to comment.