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

Remote validator. #127

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ For integrating with Twitter bootstrap 3 (horizontal form), this is what you nee
});

var bootstrapField = function (name, object) {

object.widget.classes = object.widget.classes || [];
object.widget.classes.push('form-control');

Expand Down Expand Up @@ -217,6 +216,23 @@ Forms can be created with an optional "options" object as well.
* `validatePastFirstError`: `true`, otherwise assumes `false`
* If `false`, the first validation error will halt form validation.
* If `true`, all fields will be validated.
* `remoteValidator`: function that returns an object of validation, eg by making the http request. If it is defined fields validation will be ignored. An example:

```
var remoteValidator = function(data, next) {
var response = {
field1: {
error: 'validation error 1',
value: 'wrongInput1'
},
field2: {
error: 'validation error 2',
value: 'wrongInput2'
}
};
next(response);
}
```


### Form object
Expand Down
1 change: 0 additions & 1 deletion lib/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,3 @@ exports.date = function (opt) {
exports.object = function (fields, opts) {
return forms.create(fields || {});
};

20 changes: 18 additions & 2 deletions lib/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ exports.create = function (fields, opts) {
var b = {};
b.toHTML = f.toHTML;
b.fields = {};
b.remoteValidator = f.remoteValidator;

Object.keys(f.fields).forEach(function (k) {
if (data != null) {
b.fields[k] = f.fields[k].bind(data[k]);
Expand All @@ -47,6 +49,20 @@ exports.create = function (fields, opts) {
callback = arguments[0];
}

if(b.remoteValidator) {
b.remoteValidator(b.data, (function(b) {
return function (err, response) {
var form = b;
Object.keys(form.fields).forEach(function (fieldName) {
form.fields[fieldName].error = response[fieldName].error;
form.fields[fieldName].value = response[fieldName].value;
});
callback(err, form);
};
})(b));
return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would you want/need to bypass default validators when setting the remote one? If you don't want local validators, you wouldn't set any

}

async.forEach(Object.keys(b.fields), function (k, callback) {
b.fields[k].validate(b, function (err, bound_field) {
b.fields[k] = bound_field;
Expand Down Expand Up @@ -111,8 +127,8 @@ exports.create = function (fields, opts) {
var kname = is.string(name) ? name + '[' + k + ']' : k;
return html + form.fields[k].toHTML(kname, iterator);
}, '');
}
},
remoteValidator: opts.remoteValidator
};
return f;
};

91 changes: 90 additions & 1 deletion test/test-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,96 @@ test('validate invalid data', function (t) {
});
});

test('validate valid data with remote validator', function (t) {
t.plan(2);
var fields = {
field1: forms.fields.string(),
field2: forms.fields.string()
};

var options = {
remoteValidator: function(data, next) {
setTimeout(function() {
var response = {
field1: {
error: '',
value: 'field1 value'
},
field2: {
error: '',
value: 'field2 value'
}
};
next(null, response);
}, 100);
}
};

var f = forms.create(fields, options);

f.bind({field1: '1', field2: '2'}).validate(function (err, f) {
t.equal(f.isValid(), false);
t.equal(
f.toHTML(),
'<div class="field">' +
'<label for="id_field1">Field1</label>' +
'<input type="text" name="field1" id="id_field1" value="field1 value" />' +
'</div>' +
'<div class="field">' +
'<label for="id_field2">Field2</label>' +
'<input type="text" name="field2" id="id_field2" value="field2 value" />' +
'</div>'
);
t.end();
});
});

test('validate invalid data with remote validator', function (t) {
t.plan(2);
var fields = {
field1: forms.fields.string(),
field2: forms.fields.string()
};

var options = {
remoteValidator: function(data, next) {
setTimeout(function() {
var response = {
field1: {
error: 'validation error 1',
value: 'wrongInput1'
},
field2: {
error: 'validation error 2',
value: 'wrongInput2'
}
};
next(null, response);
}, 100);
}
};

var f = forms.create(fields, options);

f.bind({field1: '1', field2: '2'}).validate(function (err, f) {
t.equal(f.isValid(), false);
t.equal(
f.toHTML(),
'<div class="field error">' +
'<p class="error_msg">validation error 1</p>' +
'<label for="id_field1">Field1</label>' +
'<input type="text" name="field1" id="id_field1" value="wrongInput1" />' +
'</div>' +
'<div class="field error">' +
'<p class="error_msg">validation error 2</p>' +
'<label for="id_field2">Field2</label>' +
'<input type="text" name="field2" id="id_field2" value="wrongInput2" />' +
'</div>'
);
t.end();
});
});

test('handle empty', function (t) {
t.plan(3);
var f = forms.create({field1: forms.fields.string()});
Expand Down Expand Up @@ -444,4 +534,3 @@ test('div bound error', function (t) {
t.end();
});
});