Skip to content

Commit

Permalink
Fix #38 - match using modelView
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Sharp committed Apr 29, 2016
1 parent 7626171 commit 13a885a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 17 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-validation-match",
"version": "1.7.1",
"version": "1.8.0",
"homepage": "https://github.com/TheSharpieOne/angular-validation-match",
"authors": [
"TheSharpieOne <evan@lostonia.com>"
Expand Down
13 changes: 7 additions & 6 deletions dist/angular-validation-match.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
* angular-validation-match
* Checks if one input matches another
* @version v1.7.1
* @version v1.8.0
* @link https://github.com/TheSharpieOne/angular-validation-match
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
Expand Down Expand Up @@ -30,19 +30,20 @@ function match ($parse) {
ctrl.$$parseAndValidate();
});

ctrl.$validators.match = function(){
ctrl.$validators.match = function(modelValue, viewValue){
var matcher = modelValue || viewValue;
var match = getMatchValue();
var notMatch = noMatchGetter(scope);
var value;

if (matchIgnoreEmptyGetter(scope) && !ctrl.$viewValue) {
if (matchIgnoreEmptyGetter(scope) && !viewValue) {
return true;
}

if(caselessGetter(scope)){
value = angular.lowercase(ctrl.$viewValue) === angular.lowercase(match);
value = angular.lowercase(matcher) === angular.lowercase(match);
}else{
value = ctrl.$viewValue === match;
value = matcher === match;
}
/*jslint bitwise: true */
value ^= notMatch;
Expand All @@ -60,4 +61,4 @@ function match ($parse) {
}
};
}
})(window, window.angular);
})(window, window.angular);
4 changes: 2 additions & 2 deletions dist/angular-validation-match.min.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*!
* angular-validation-match
* Checks if one input matches another
* @version v1.7.1
* @version v1.8.0
* @link https://github.com/TheSharpieOne/angular-validation-match
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
!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);
!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);
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-validation-match",
"version": "1.7.1",
"version": "1.8.0",
"homepage": "https://github.com/TheSharpieOne/angular-validation-match",
"description": "Checks if one input matches another",
"main": "./index.js",
Expand Down Expand Up @@ -30,7 +30,7 @@
}
],
"license": "MIT",
"dependencies":{
"dependencies": {
"angular": ">=1.3.0 <2.0.0"
},
"devDependencies": {
Expand All @@ -49,9 +49,9 @@
"karma-chrome-launcher": "^0.2.2",
"karma-firefox-launcher": "^0.1.0",
"karma-mocha": "^0.2.1",
"karma-phantomjs-launcher": "^0.2.1",
"karma-phantomjs-launcher": "^1.0.0",
"mocha": "^2.3.4",
"phantomjs": "^1.9.19",
"phantomjs-prebuilt": "^2.1.7",
"sinon": "^1.16.1",
"sinon-chai": "^2.8.0"
}
Expand Down
9 changes: 5 additions & 4 deletions src/angular-validation-match.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ function match ($parse) {
ctrl.$$parseAndValidate();
});

ctrl.$validators.match = function(){
ctrl.$validators.match = function(modelValue, viewValue){
var matcher = modelValue || viewValue;
var match = getMatchValue();
var notMatch = noMatchGetter(scope);
var value;

if (matchIgnoreEmptyGetter(scope) && !ctrl.$viewValue) {
if (matchIgnoreEmptyGetter(scope) && !viewValue) {
return true;
}

if(caselessGetter(scope)){
value = angular.lowercase(ctrl.$viewValue) === angular.lowercase(match);
value = angular.lowercase(matcher) === angular.lowercase(match);
}else{
value = ctrl.$viewValue === match;
value = matcher === match;
}
/*jslint bitwise: true */
value ^= notMatch;
Expand Down
73 changes: 73 additions & 0 deletions test/angular-input-match.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,77 @@ describe('Directives: validation - match', function() {
});

});

describe('non-text validation', function() {

describe('behavior:', function() {
var validTemplate = '<input ng-model="confirmation" type="number" match="original"></input>';

it('returns true if no model value has been defined', function() {
compiled = $compile(validTemplate)($scope);
expect($scope.confirmation).to.be.undefined();
$scope.$digest();
expect(compiled.hasClass('ng-valid')).to.be.true();
});


it('returns true if $modelValue are identical', function() {
$scope.confirmation = 1;
compiled = $compile(validTemplate)($scope);
$scope.original = 1;
$scope.$digest();
expect(compiled.hasClass('ng-valid')).to.be.true();
});

it('returns false if $modelValue are a different type', function() {
$scope.confirmation = 1;
$scope.original = "1";
compiled = $compile(validTemplate)($scope);
$scope.$digest();
expect(compiled.hasClass('ng-invalid')).to.be.true();
});

});


describe('Form level validation', function() {
var form,
element,
inputValue = -102;

beforeEach(function() {
element = angular.element(
'<form name="form">' +
'<input type="number" ng-model="test" name="test"></input>' +
'<input type="number" match="test" ng-model="testConfirm" name="testConfirm"></input>' +
'</form>'
);
$scope.test = inputValue;
$compile(element)($scope);
$scope.$digest();
form = $scope.form;
});

it('should check if values are identical', function() {
form.testConfirm.$setViewValue(inputValue);
$scope.$digest();
expect(form.testConfirm.$error.match).to.be.undefined();
});

it('should check if values are not identical', function() {
form.testConfirm.$setViewValue(inputValue+1);
$scope.$digest();
expect(form.testConfirm.$error.match).to.be.true();
});

it('should check if values are not identical because of different types', function() {
form.test.$setViewValue(inputValue+"string");
form.testConfirm.$setViewValue(inputValue);
$scope.$digest();
expect(form.testConfirm.$error.match).to.be.true();
});

});

});
});

0 comments on commit 13a885a

Please sign in to comment.