Skip to content

Commit

Permalink
Form-wide _error wasn't flipping valid flag (#1606)
Browse files Browse the repository at this point in the history
* Form-wide _error wasn't flipping valid flag

* No longer need hasErrors. Consolidated hasError() logic from #1508.
  • Loading branch information
erikras committed Aug 24, 2016
1 parent 3fd2dfc commit 2133ec7
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 226 deletions.
82 changes: 0 additions & 82 deletions src/__tests__/hasErrors.spec.js

This file was deleted.

4 changes: 4 additions & 0 deletions src/__tests__/reduxForm.spec.js
Expand Up @@ -2292,11 +2292,15 @@ const describeReduxForm = (name, structure, combineReducers, expect) => {
expect(formRender).toHaveBeenCalled()
expect(formRender.calls.length).toBe(2)
expect(formRender.calls[ 1 ].arguments[ 0 ].error).toBe('form wide sync error')
expect(formRender.calls[ 1 ].arguments[ 0 ].valid).toBe(false)
expect(formRender.calls[ 1 ].arguments[ 0 ].invalid).toBe(true)

input.calls[0].arguments[0].input.onChange('bar')

expect(formRender.calls.length).toBe(4)
expect(formRender.calls[ 3 ].arguments[ 0 ].error).toNotExist()
expect(formRender.calls[ 3 ].arguments[ 0 ].valid).toBe(true)
expect(formRender.calls[ 3 ].arguments[ 0 ].invalid).toBe(false)
})

it('should allow for sync errors to be objects', () => {
Expand Down
32 changes: 6 additions & 26 deletions src/hasError.js
Expand Up @@ -11,36 +11,16 @@ const getErrorKeys = (name, type) => {

const createHasError = ({ getIn }) => {
const hasError = (field, syncErrors, asyncErrors, submitErrors) => {
const name = getIn(field, 'name')
const type = getIn(field, 'type')
if (!syncErrors && !asyncErrors && !submitErrors) {
return false
}
const errorKeys = getErrorKeys(name, type)

const syncError = errorKeys.reduce((error, errorKey) => {
const curError = plainGetIn(syncErrors, errorKey)
return curError ? error + curError : error
}, null)
if (syncError != null) {
return true
}
const asyncError = errorKeys.reduce((error, errorKey) => {
const curError = getIn(asyncErrors, errorKey)
return curError ? error + curError : error
}, null)
if (asyncError != null) {
return true
}
const submitError = errorKeys.reduce((error, errorKey) => {
const curError = getIn(submitErrors, errorKey)
return curError ? error + curError : error
}, null)
if (submitError != null) {
return true
}

return false
const name = getIn(field, 'name')
const type = getIn(field, 'type')
return getErrorKeys(name, type).some(key =>
plainGetIn(syncErrors, key) ||
getIn(asyncErrors, key) ||
getIn(submitErrors, key))
}
return hasError
}
Expand Down
18 changes: 0 additions & 18 deletions src/hasErrors.js

This file was deleted.

47 changes: 3 additions & 44 deletions src/selectors/__tests__/isInvalid.spec.js
Expand Up @@ -99,22 +99,21 @@ const describeIsInvalid = (name, structure, expect) => {
}))).toBe(true)
})

it('should return true when there is a global sync error', () => {
expect(isInvalid('foo')(setIn(fromJS({
it('should return true when there is a global error', () => {
expect(isInvalid('foo')(fromJS({
form: {
foo: {
values: {
dog: 'Odie',
cat: 'Garfield'
},
error: 'Bad Data',
registeredFields: [
{ name: 'dog', type: 'Field' },
{ name: 'cat', type: 'Field' }
]
}
}
}), 'form.foo.syncErrors', {
_error: 'Bad data'
}))).toBe(true)
})

Expand Down Expand Up @@ -180,26 +179,6 @@ const describeIsInvalid = (name, structure, expect) => {
}))).toBe(true)
})

it('should return true when there is a global async error', () => {
expect(isInvalid('foo')(fromJS({
form: {
foo: {
values: {
dog: 'Odie',
cat: 'Garfield'
},
registeredFields: [
{ name: 'dog', type: 'Field' },
{ name: 'cat', type: 'Field' }
],
asyncErrors: {
_error: 'Bad data'
}
}
}
}))).toBe(true)
})

it('should return false when there are submit errors for a NON-registered field', () => {
expect(isInvalid('foo')(fromJS({
form: {
Expand Down Expand Up @@ -262,26 +241,6 @@ const describeIsInvalid = (name, structure, expect) => {
}))).toBe(true)
})

it('should return true when there is a global submit error', () => {
expect(isInvalid('foo')(fromJS({
form: {
foo: {
values: {
dog: 'Odie',
cat: 'Garfield'
},
registeredFields: [
{ name: 'dog', type: 'Field' },
{ name: 'cat', type: 'Field' }
],
submitErrors: {
_error: 'Bad data'
}
}
}
}))).toBe(true)
})

it('should use getFormState if provided', () => {
expect(isInvalid('foo', state => getIn(state, 'someOtherSlice'))(fromJS({
someOtherSlice: {
Expand Down
47 changes: 3 additions & 44 deletions src/selectors/__tests__/isValid.spec.js
Expand Up @@ -99,22 +99,21 @@ const describeIsValid = (name, structure, expect) => {
}))).toBe(false)
})

it('should return false when there is a global sync error', () => {
expect(isValid('foo')(setIn(fromJS({
it('should return false when there is a global error', () => {
expect(isValid('foo')(fromJS({
form: {
foo: {
values: {
dog: 'Odie',
cat: 'Garfield'
},
error: 'Bad data',
registeredFields: [
{ name: 'dog', type: 'Field' },
{ name: 'cat', type: 'Field' }
]
}
}
}), 'form.foo.syncErrors', {
_error: 'Bad data'
}))).toBe(false)
})

Expand Down Expand Up @@ -180,26 +179,6 @@ const describeIsValid = (name, structure, expect) => {
}))).toBe(false)
})

it('should return false when there is a global async error', () => {
expect(isValid('foo')(fromJS({
form: {
foo: {
values: {
dog: 'Odie',
cat: 'Garfield'
},
registeredFields: [
{ name: 'dog', type: 'Field' },
{ name: 'cat', type: 'Field' }
],
asyncErrors: {
_error: 'Bad data'
}
}
}
}))).toBe(false)
})

it('should return true when there are submit errors for a NON-registered field', () => {
expect(isValid('foo')(fromJS({
form: {
Expand Down Expand Up @@ -262,26 +241,6 @@ const describeIsValid = (name, structure, expect) => {
}))).toBe(false)
})

it('should return false when there is a global submit error', () => {
expect(isValid('foo')(fromJS({
form: {
foo: {
values: {
dog: 'Odie',
cat: 'Garfield'
},
registeredFields: [
{ name: 'dog', type: 'Field' },
{ name: 'cat', type: 'Field' }
],
submitErrors: {
_error: 'Bad data'
}
}
}
}))).toBe(false)
})

it('should use getFormState if provided', () => {
expect(isValid('foo', state => getIn(state, 'someOtherSlice'))(fromJS({
someOtherSlice: {
Expand Down
22 changes: 10 additions & 12 deletions src/selectors/isValid.js
@@ -1,26 +1,24 @@
import createHasErrors from '../hasErrors'
import createHasError from '../hasError'
import plain from '../structure/plain'

const plainHasErrors = createHasErrors(plain)

const createIsValid = structure => {
const { getIn } = structure
const hasErrors = createHasErrors(structure)
const hasError = createHasError(structure)
return (form, getFormState = state => getIn(state, 'form')) =>
state => {
const formState = getFormState(state)
const error = getIn(formState, `${form}.error`)
if (error) {
return false
}
const syncErrors = getIn(formState, `${form}.syncErrors`)
const asyncErrors = getIn(formState, `${form}.asyncErrors`)
const submitErrors = getIn(formState, `${form}.submitErrors`)
const syncErrors = getIn(formState, `${form}.syncErrors`)
const hasSyncErrors = plainHasErrors(syncErrors)
const hasAsyncErrors = hasErrors(asyncErrors)
const hasSubmitErrors = hasErrors(submitErrors)
if(!syncErrors && !asyncErrors && !submitErrors) {
return true
}

const registeredFields = getIn(formState, `${form}.registeredFields`) || []
const hasFieldWithError = registeredFields &&
registeredFields.some(field => hasError(field, syncErrors, asyncErrors, submitErrors))
return !hasSyncErrors && !hasAsyncErrors && !hasSubmitErrors && !hasFieldWithError
return !registeredFields.some(field => hasError(field, syncErrors, asyncErrors, submitErrors))
}
}

Expand Down

0 comments on commit 2133ec7

Please sign in to comment.