Skip to content

Commit

Permalink
fix(forms): fix dependsOn
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Nov 11, 2016
1 parent 37a6c53 commit c06f614
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
18 changes: 14 additions & 4 deletions packages/cerebral-forms/src/factories/validateField.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export default function validateFieldFactory (path) {
const formPath = fieldPath.slice().splice(0, fieldPath.length - 1)
const field = state.get(fieldPath)
const form = state.get(formPath)
const validationResult = runValidation(fieldPath, field, form)

state.merge(fieldPath, runValidation(fieldPath, field, form))
state.merge(fieldPath, validationResult)

let dependentFields = []
if (Array.isArray(field.dependsOn)) {
Expand All @@ -33,7 +34,7 @@ export default function validateFieldFactory (path) {
dependentFields = [field.dependsOn]
}

dependentFields.forEach((stringPath) => {
const depententOfValidationResult = dependentFields.reduce((currentValidationResult, stringPath) => {
const dependentFieldPath = stringPath.split('.')
const dependentFormPath = dependentFieldPath.slice().splice(0, dependentFieldPath.length - 1)
const field = state.get(dependentFieldPath)
Expand All @@ -43,8 +44,17 @@ export default function validateFieldFactory (path) {
throw new Error(`The path ${stringPath} used with "dependsOn" on field ${fieldPath.join('.')} is not correct, please check it`)
}

state.merge(fieldPath, runValidation(dependentFieldPath, field, form))
})
const dependentValidationResult = runValidation(dependentFieldPath, field, form)
state.merge(dependentFieldPath, dependentValidationResult)

if (currentValidationResult.isValid && !dependentValidationResult.isValid) {
return dependentValidationResult
}

return currentValidationResult
}, validationResult)

state.merge(fieldPath, depententOfValidationResult)
}

return validateField
Expand Down
8 changes: 4 additions & 4 deletions packages/cerebral-forms/src/field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ describe('field', () => {
password: {
value: '',
isRequired: true,
isValueRules: ['isValue'],
requiredMessage: 'Password is required.',
validationRules: [
'minLength:15'
Expand All @@ -76,7 +75,6 @@ describe('field', () => {
repeatPassword: {
value: '',
isRequired: true,
isValueRules: ['isValue'],
validationRules: [
'equalsField:password'
],
Expand All @@ -93,8 +91,10 @@ describe('field', () => {
}
})
controller.getSignal('fieldChanged')({field: 'form.password', value: 'abcdefghijklmnopqrstuvxyz'})
assert.equal(controller.getState('form.password.isValid'), false)
assert.equal(controller.getState('form.repeatPassword.isValid'), false)
controller.getSignal('fieldChanged')({field: 'form.repeatPassword', value: 'abcdefghijklmnopqrstuvxyz'})
let state = controller.getState()
assert(state.form.password.isValid, true)
assert.equal(controller.getState('form.password.isValid'), true)
assert.equal(controller.getState('form.repeatPassword.isValid'), true)
})
})

0 comments on commit c06f614

Please sign in to comment.