Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Deep validation on submit #541

Merged
merged 2 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/components/form-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 10 additions & 0 deletions test/form-component-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,7 @@ Object.keys(testContexts).forEach((testKey) => {
</Form>
</Provider>
);
const formElement = TestUtils.findRenderedDOMComponentWithTag(form, 'form');

const [_, input2] = TestUtils
.scryRenderedDOMComponentsWithTag(form, 'input');
Expand All @@ -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);
Expand Down