Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug where .bind with incomplete data was removing fields from form. #107

Merged
merged 1 commit into from
Mar 17, 2014
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ exports.create = function (fields) {
b.toHTML = f.toHTML;
b.fields = {};
Object.keys(f.fields).forEach(function (k) {
if (data[k] != null) {
if (data != null) {
b.fields[k] = f.fields[k].bind(data[k]);
}
});
Expand Down
28 changes: 28 additions & 0 deletions test/test-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ test('bind', function (t) {
t.end();
});

test('bind with missing field in data keeps field in form', function (t) {
t.plan(12);
var form = forms.create({
field1: forms.fields.string(),
field2: forms.fields.string()
});
// unbound
t.equal(form.isValid, undefined);

// bound
var f = form.bind({field1: 'data one'});
t.equal(f.fields.field1.value, 'data one');
t.equal(f.fields.field1.data, 'data one');
t.equal(f.fields.field1.error, undefined);
t.equal(f.fields.field2.value, undefined);
t.equal(f.fields.field2.data, '');
t.equal(f.fields.field2.error, undefined);

t.ok(f.isValid instanceof Function);
t.equal(f.bind, undefined);
t.equal(f.handle, undefined);

t.deepEqual(f.data, {field1: 'data one', field2: ''});
t.ok(form !== f, 'bind returns new form object');

t.end();
});

test('validate', function (t) {
t.plan(11);
var form = forms.create({
Expand Down