Skip to content

Commit

Permalink
Adding requiresFieldIfEmpty validator.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 28, 2013
1 parent 8fc81ce commit c6df2a6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ components following the same API.
### Validators

* matchField
* requiresFieldIfEmpty
* min
* max
* range
Expand Down
6 changes: 6 additions & 0 deletions example/complex.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ var form = forms.create({
required: true,
validators: [validators.matchField('password')]
}),
phone_1: fields.string({
validators: [validators.requiresFieldIfEmpty('phone_2')]
}),
phone_2: fields.string({
validators: [validators.requiresFieldIfEmpty('phone_1')]
}),
options: fields.string({
choices: {
one: 'option one',
Expand Down
15 changes: 15 additions & 0 deletions lib/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ exports.matchField = function (match_field, message) {
};
};

exports.requiresFieldIfEmpty = function (alternate_field, message) {
if (!message) { message = 'One of %s or %s is required.'; }
var validator = function (form, field, callback) {
var alternateBlank = trim(form.fields[alternate_field].data).length === 0,
fieldBlank = trim(field.data).length === 0;
if (alternateBlank && fieldBlank) {
callback(util.format(message, field.name, alternate_field));
} else {
callback();
}
};
validator.forceValidation = true;
return validator;
};

exports.min = function (val, message) {
if (!message) { message = 'Please enter a value greater than or equal to %s.'; }
return function (form, field, callback) {
Expand Down
33 changes: 33 additions & 0 deletions test/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,39 @@ exports.matchField = function (test) {
});
};

exports.requiresFieldIfEmpty = function (test) {
var v = validators.requiresFieldIfEmpty('alternate_field', 'field 1: %s field2: %s'),
empty_fields = {
field: {name: 'field', data: ' '},
alternate_field: {name: 'alternate_field', data: ''}
},
filled_fields = {
field: {name: 'field', data: 'filled'},
alternate_field: {name: 'alternate_field', data: 'also filled'}
},
first_filled = {
field: {name: 'field', data: 'filled'},
alternate_field: {name: 'alternate_field', data: ''}
},
second_filled = {
field: {name: 'field', data: ''},
alternate_field: {name: 'alternate_field', data: 'filled'}
};
v({ fields: empty_fields }, empty_fields.field, function (err) {
test.equals(err, 'field 1: field field2: alternate_field');
v({ fields: filled_fields }, filled_fields.field, function (err) {
test.equals(err, undefined);
v({ fields: first_filled }, first_filled.field, function (err) {
test.equals(err, undefined);
v({ fields: second_filled }, second_filled.field, function (err) {
test.equals(err, undefined);
test.done();
});
});
});
});
};

exports.min = function (test) {
validators.min(100, 'Value must be greater than or equal to %s.')('form', {data: 50}, function (err) {
test.equals(err, 'Value must be greater than or equal to 100.');
Expand Down

0 comments on commit c6df2a6

Please sign in to comment.