-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved processSubmitErrors to render errors for every children in e…
…rrors tree
- Loading branch information
Showing
1 changed file
with
24 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |