Skip to content

Commit 13a885a

Browse files
author
Evan Sharp
committed
Fix #38 - match using modelView
1 parent 7626171 commit 13a885a

File tree

6 files changed

+92
-17
lines changed

6 files changed

+92
-17
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-validation-match",
3-
"version": "1.7.1",
3+
"version": "1.8.0",
44
"homepage": "https://github.com/TheSharpieOne/angular-validation-match",
55
"authors": [
66
"TheSharpieOne <evan@lostonia.com>"

dist/angular-validation-match.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-validation-match
33
* Checks if one input matches another
4-
* @version v1.7.1
4+
* @version v1.8.0
55
* @link https://github.com/TheSharpieOne/angular-validation-match
66
* @license MIT License, http://www.opensource.org/licenses/MIT
77
*/
@@ -30,19 +30,20 @@ function match ($parse) {
3030
ctrl.$$parseAndValidate();
3131
});
3232

33-
ctrl.$validators.match = function(){
33+
ctrl.$validators.match = function(modelValue, viewValue){
34+
var matcher = modelValue || viewValue;
3435
var match = getMatchValue();
3536
var notMatch = noMatchGetter(scope);
3637
var value;
3738

38-
if (matchIgnoreEmptyGetter(scope) && !ctrl.$viewValue) {
39+
if (matchIgnoreEmptyGetter(scope) && !viewValue) {
3940
return true;
4041
}
4142

4243
if(caselessGetter(scope)){
43-
value = angular.lowercase(ctrl.$viewValue) === angular.lowercase(match);
44+
value = angular.lowercase(matcher) === angular.lowercase(match);
4445
}else{
45-
value = ctrl.$viewValue === match;
46+
value = matcher === match;
4647
}
4748
/*jslint bitwise: true */
4849
value ^= notMatch;
@@ -60,4 +61,4 @@ function match ($parse) {
6061
}
6162
};
6263
}
63-
})(window, window.angular);
64+
})(window, window.angular);

dist/angular-validation-match.min.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*!
22
* angular-validation-match
33
* Checks if one input matches another
4-
* @version v1.7.1
4+
* @version v1.8.0
55
* @link https://github.com/TheSharpieOne/angular-validation-match
66
* @license MIT License, http://www.opensource.org/licenses/MIT
77
*/
8-
!function(e,a,t){"use strict";function i(e){return{require:"?ngModel",restrict:"A",link:function(t,i,n,r){function c(){var e=o(t);return a.isObject(e)&&e.hasOwnProperty("$viewValue")&&(e=e.$viewValue),e}if(r){var o=e(n.match),u=e(n.matchCaseless),l=e(n.notMatch),s=e(n.matchIgnoreEmpty);t.$watch(c,function(){r.$$parseAndValidate()}),r.$validators.match=function(){var e,i=c(),n=l(t);return s(t)&&!r.$viewValue?!0:(e=u(t)?a.lowercase(r.$viewValue)===a.lowercase(i):r.$viewValue===i,e^=n,!!e)}}}}}i.$inject=["$parse"],a.module("validation.match",[]),a.module("validation.match").directive("match",i)}(window,window.angular);
8+
!function(t,a,e){"use strict";function n(t){return{require:"?ngModel",restrict:"A",link:function(e,n,i,r){function c(){var t=o(e);return a.isObject(t)&&t.hasOwnProperty("$viewValue")&&(t=t.$viewValue),t}if(r){var o=t(i.match),u=t(i.matchCaseless),l=t(i.notMatch),s=t(i.matchIgnoreEmpty);e.$watch(c,function(){r.$$parseAndValidate()}),r.$validators.match=function(t,n){var i,r=t||n,o=c(),d=l(e);return s(e)&&!n?!0:(i=u(e)?a.lowercase(r)===a.lowercase(o):r===o,i^=d,!!i)}}}}}n.$inject=["$parse"],a.module("validation.match",[]),a.module("validation.match").directive("match",n)}(window,window.angular);

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-validation-match",
3-
"version": "1.7.1",
3+
"version": "1.8.0",
44
"homepage": "https://github.com/TheSharpieOne/angular-validation-match",
55
"description": "Checks if one input matches another",
66
"main": "./index.js",
@@ -30,7 +30,7 @@
3030
}
3131
],
3232
"license": "MIT",
33-
"dependencies":{
33+
"dependencies": {
3434
"angular": ">=1.3.0 <2.0.0"
3535
},
3636
"devDependencies": {
@@ -49,9 +49,9 @@
4949
"karma-chrome-launcher": "^0.2.2",
5050
"karma-firefox-launcher": "^0.1.0",
5151
"karma-mocha": "^0.2.1",
52-
"karma-phantomjs-launcher": "^0.2.1",
52+
"karma-phantomjs-launcher": "^1.0.0",
5353
"mocha": "^2.3.4",
54-
"phantomjs": "^1.9.19",
54+
"phantomjs-prebuilt": "^2.1.7",
5555
"sinon": "^1.16.1",
5656
"sinon-chai": "^2.8.0"
5757
}

src/angular-validation-match.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@ function match ($parse) {
2222
ctrl.$$parseAndValidate();
2323
});
2424

25-
ctrl.$validators.match = function(){
25+
ctrl.$validators.match = function(modelValue, viewValue){
26+
var matcher = modelValue || viewValue;
2627
var match = getMatchValue();
2728
var notMatch = noMatchGetter(scope);
2829
var value;
2930

30-
if (matchIgnoreEmptyGetter(scope) && !ctrl.$viewValue) {
31+
if (matchIgnoreEmptyGetter(scope) && !viewValue) {
3132
return true;
3233
}
3334

3435
if(caselessGetter(scope)){
35-
value = angular.lowercase(ctrl.$viewValue) === angular.lowercase(match);
36+
value = angular.lowercase(matcher) === angular.lowercase(match);
3637
}else{
37-
value = ctrl.$viewValue === match;
38+
value = matcher === match;
3839
}
3940
/*jslint bitwise: true */
4041
value ^= notMatch;

test/angular-input-match.spec.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,77 @@ describe('Directives: validation - match', function() {
349349
});
350350

351351
});
352+
353+
describe('non-text validation', function() {
354+
355+
describe('behavior:', function() {
356+
var validTemplate = '<input ng-model="confirmation" type="number" match="original"></input>';
357+
358+
it('returns true if no model value has been defined', function() {
359+
compiled = $compile(validTemplate)($scope);
360+
expect($scope.confirmation).to.be.undefined();
361+
$scope.$digest();
362+
expect(compiled.hasClass('ng-valid')).to.be.true();
363+
});
364+
365+
366+
it('returns true if $modelValue are identical', function() {
367+
$scope.confirmation = 1;
368+
compiled = $compile(validTemplate)($scope);
369+
$scope.original = 1;
370+
$scope.$digest();
371+
expect(compiled.hasClass('ng-valid')).to.be.true();
372+
});
373+
374+
it('returns false if $modelValue are a different type', function() {
375+
$scope.confirmation = 1;
376+
$scope.original = "1";
377+
compiled = $compile(validTemplate)($scope);
378+
$scope.$digest();
379+
expect(compiled.hasClass('ng-invalid')).to.be.true();
380+
});
381+
382+
});
383+
384+
385+
describe('Form level validation', function() {
386+
var form,
387+
element,
388+
inputValue = -102;
389+
390+
beforeEach(function() {
391+
element = angular.element(
392+
'<form name="form">' +
393+
'<input type="number" ng-model="test" name="test"></input>' +
394+
'<input type="number" match="test" ng-model="testConfirm" name="testConfirm"></input>' +
395+
'</form>'
396+
);
397+
$scope.test = inputValue;
398+
$compile(element)($scope);
399+
$scope.$digest();
400+
form = $scope.form;
401+
});
402+
403+
it('should check if values are identical', function() {
404+
form.testConfirm.$setViewValue(inputValue);
405+
$scope.$digest();
406+
expect(form.testConfirm.$error.match).to.be.undefined();
407+
});
408+
409+
it('should check if values are not identical', function() {
410+
form.testConfirm.$setViewValue(inputValue+1);
411+
$scope.$digest();
412+
expect(form.testConfirm.$error.match).to.be.true();
413+
});
414+
415+
it('should check if values are not identical because of different types', function() {
416+
form.test.$setViewValue(inputValue+"string");
417+
form.testConfirm.$setViewValue(inputValue);
418+
$scope.$digest();
419+
expect(form.testConfirm.$error.match).to.be.true();
420+
});
421+
422+
});
423+
424+
});
352425
});

0 commit comments

Comments
 (0)