Skip to content

Commit

Permalink
Type error fix for empty string
Browse files Browse the repository at this point in the history
When input value is a empty string of spaces, date validator returns
null, and `formatted = date.format(format);` gives a type error. This
commits returns false on such cases.

Fixed indent.
  • Loading branch information
SergioCrisostomo committed Jun 22, 2014
1 parent a586f51 commit df950d0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Source/Forms/Form.Validator.js
Expand Up @@ -455,13 +455,15 @@ Form.Validator.addAllThese([
value = element.get('value'),
wordsInValue = value.match(/[a-z]+/gi);

if (wordsInValue && !wordsInValue.every(dateNouns.exec, dateNouns)) return false;
if (wordsInValue && !wordsInValue.every(dateNouns.exec, dateNouns)) return false;

var date = Date.parse(value),
format = props.dateFormat || '%x',
formatted = date.format(format);
if (formatted != 'invalid date') element.set('value', formatted);
return date.isValid();
var date = Date.parse(value);
if (!date) return false;

var format = props.dateFormat || '%x',
formatted = date.format(format);
if (formatted != 'invalid date') element.set('value', formatted);
return date.isValid();
}
}],

Expand Down
4 changes: 4 additions & 0 deletions Specs/Forms/Form.Validator.js
Expand Up @@ -314,6 +314,10 @@ describe('Form.Validator', function(){
expect(validator.test(createInput('Boo 12'))).toEqual(false);
});

it('should return false, instead of Type Error, when passed a empty string', function(){
expect(validator.test(createInput(' '))).toBeFalsy()
});

it('should return true for fields whose value parses to a date', function(){
expect(validator.test(createInput('Nov 12'))).toEqual(true);
expect(validator.test(createInput('10-10-2000'))).toEqual(true);
Expand Down

0 comments on commit df950d0

Please sign in to comment.