Skip to content

Commit

Permalink
Merge pull request #106 from timjrobinson/nested_fields_fix
Browse files Browse the repository at this point in the history
Fixed null object error when a nested form is submitted but one of the subsections is missing.
  • Loading branch information
ljharb committed Mar 16, 2014
2 parents ffc092a + 5169858 commit 2b6dd73
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ exports.create = function (fields) {
b.toHTML = f.toHTML;
b.fields = {};
Object.keys(f.fields).forEach(function (k) {
b.fields[k] = f.fields[k].bind(data[k]);
if (data[k] != null) {
b.fields[k] = f.fields[k].bind(data[k]);
}
});
b.data = Object.keys(b.fields).reduce(function (a, k) {
a[k] = b.fields[k].data;
Expand Down
10 changes: 10 additions & 0 deletions test/test-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ test('handle empty object', function (t) {
t.end();
});

test('handle missing multi-form section', function (t) {
t.plan(1);
var f = forms.create({
section1: {field1: forms.fields.string()},
section2: {field1: forms.fields.string()}
});
f.bind({section1: {field1: "string"}});
t.ok(true, 'Form handled missing section ok.');
});

test('handle error', function (t) {
t.plan(5);
var f = forms.create({field1: forms.fields.string()});
Expand Down

0 comments on commit 2b6dd73

Please sign in to comment.