Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Length-based validation + "password" is correct input field type #601

Closed
wants to merge 5 commits into from
Closed
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
14 changes: 13 additions & 1 deletion src/widget/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ var INTEGER_REGEXP = /^\s*(\-|\+)?\d+\s*$/;
* @param {string} ng:model Assignable angular expression to data-bind to.
* @param {string=} name Property name of the form under which the widgets is published.
* @param {string=} required Sets `REQUIRED` validation error key if the value is not entered.
* @param {number=} ng:minlength Sets `MINLENGTH` validation error key if the value is shorter than minlength.
* @param {number=} ng:maxlength Sets `MAXLENGTH` validation error key if the value is longer than maxlength.
* @param {string=} ng:pattern Sets `PATTERN` validation error key if the value does not match the
* RegExp pattern expression. Expected value is `/regexp/` for inline patterns or `regexp` for
* patterns defined as scope expressions.
Expand Down Expand Up @@ -656,14 +658,16 @@ angularWidget('input', function(inputElement){
modelScope = this,
patternMatch, widget,
pattern = trim(inputElement.attr('ng:pattern')),
minlength = parseInt(inputElement.attr('ng:minlength'), 10),
maxlength = parseInt(inputElement.attr('ng:maxlength'), 10),
loadFromScope = type.match(/^\s*\@\s*(.*)/);


if (!pattern) {
patternMatch = valueFn(true);
} else {
if (pattern.match(/^\/(.*)\/$/)) {
pattern = new RegExp(pattern.substring(1, pattern.length - 2));
pattern = new RegExp(pattern.substring(1, pattern.length - 1));
patternMatch = function(value) {
return pattern.test(value);
}
Expand Down Expand Up @@ -713,13 +717,21 @@ angularWidget('input', function(inputElement){
widget.$on('$validate', function(event) {
var $viewValue = trim(widget.$viewValue);
var inValid = widget.$required && !$viewValue;
var tooLong = maxlength && $viewValue && $viewValue.length > maxlength,
tooShort = minlength && $viewValue && $viewValue.length < minlength;
var missMatch = $viewValue && !patternMatch($viewValue);
if (widget.$error.REQUIRED != inValid){
widget.$emit(inValid ? '$invalid' : '$valid', 'REQUIRED');
}
if (widget.$error.PATTERN != missMatch){
widget.$emit(missMatch ? '$invalid' : '$valid', 'PATTERN');
}
if (widget.$error.MINLENGTH != tooShort){
widget.$emit(tooShort ? '$invalid' : '$valid', 'MINLENGTH');
}
if (widget.$error.MAXLENGTH != tooLong){
widget.$emit(tooLong ? '$invalid' : '$valid', 'MAXLENGTH');
}
});

forEach(['valid', 'invalid', 'pristine', 'dirty'], function(name) {
Expand Down
11 changes: 11 additions & 0 deletions test/widget/inputSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,13 @@ describe('widget: input', function() {
});
});

describe('password', function () {
it('should not change password type to text', function () {
doc = angular.element('<form name="form"><input type="password" ng:model="name" /></form>');
scope = angular.compile(doc)();
expect(doc.find('input')[0].getAttribute('type')).toEqual('password');
});
});

it('should ignore text widget which have no name', function() {
compile('<input type="text"/>');
Expand Down Expand Up @@ -549,6 +556,10 @@ describe('widget: input', function() {
scope.regexp = /^\d\d\d-\d\d-\d\d\d\d$/;
});

itShouldVerify('text with length limits',
['aaa', 'aaaaa', 'aaaaaaaaa'],
['', 'a', 'aa', 'aaaaaaaaaa'],
{'ng:minlength': 3, 'ng:maxlength': 9});

it('should throw an error when scope pattern can\'t be found', function() {
var el = jqLite('<input ng:model="foo" ng:pattern="fooRegexp">'),
Expand Down