From 4a121d19a633d030ec0a1b7e94f779ec8f515312 Mon Sep 17 00:00:00 2001 From: Tiago Moraes Date: Thu, 17 Nov 2016 11:37:36 -0300 Subject: [PATCH 1/2] add test for submit form with deep validation --- test/form-component-spec.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/form-component-spec.js b/test/form-component-spec.js index cee7aa57d..9fd33ecf9 100644 --- a/test/form-component-spec.js +++ b/test/form-component-spec.js @@ -1641,6 +1641,7 @@ Object.keys(testContexts).forEach((testKey) => { ); + const formElement = TestUtils.findRenderedDOMComponentWithTag(form, 'form'); const [_, input2] = TestUtils .scryRenderedDOMComponentsWithTag(form, 'input'); @@ -1652,6 +1653,15 @@ Object.keys(testContexts).forEach((testKey) => { assert.isTrue($form.valid); }); + it('after submit should stay valid', () => { + TestUtils.Simulate.submit(formElement); + const { $form, items } = store.getState().testForm; + + assert.isTrue(items[0].name.valid); + assert.isTrue(items[1].name.valid); + assert.isTrue($form.valid); + }); + it('should check validity of each item on change', () => { input2.value = ''; TestUtils.Simulate.change(input2); From ea217e0baa07f14678f2806313bbdbac05f8d9a5 Mon Sep 17 00:00:00 2001 From: Tiago Moraes Date: Thu, 17 Nov 2016 12:03:38 -0300 Subject: [PATCH 2/2] fix deep form validation on submit. complement to #510 --- src/components/form-component.js | 41 ++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/components/form-component.js b/src/components/form-component.js index 15ee9b168..3baba05c6 100644 --- a/src/components/form-component.js +++ b/src/components/form-component.js @@ -284,15 +284,42 @@ function createFormClass(s = defaultStrategy) { ? merge(invertValidators(validators), errorValidators) : errorValidators; - const fieldsValidity = mapValues(finalErrorValidators, (validator, field) => { - const fieldValue = field - ? s.get(modelValue, field) - : modelValue; + const fieldsValidity = {}; - const fieldValidity = getValidity(validator, fieldValue); + // this is (internally) mutative for performance reasons. + const validateField = (validator, field) => { + if (!!~field.indexOf('[]')) { + const [parentModel, childModel] = field.split('[]'); - return fieldValidity; - }); + const fieldValue = parentModel + ? s.get(modelValue, parentModel) + : modelValue; + + fieldValue.forEach((subValue, index) => { + validateField(validator, `${parentModel}[${index}]${childModel}`); + }); + } else { + const fieldValue = field + ? s.get(modelValue, field) + : modelValue; + + const fieldValidity = getValidity(validator, fieldValue); + + fieldsValidity[field] = fieldValidity; + } + }; + + mapValues(finalErrorValidators, validateField); + + // const fieldsValidity = mapValues(finalErrorValidators, (validator, field) => { + // const fieldValue = field + // ? s.get(modelValue, field) + // : modelValue; + + // const fieldValidity = getValidity(validator, fieldValue); + + // return fieldValidity; + // }); dispatch(s.actions.batch(model, [ s.actions.setFieldsErrors(