Skip to content

Commit df17c02

Browse files
committed
feature(not-match): resolves #29
1 parent 4d77627 commit df17c02

3 files changed

Lines changed: 187 additions & 14 deletions

File tree

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@
3535
}
3636
],
3737
"devDependencies": {
38-
"chai": "1.10.0",
39-
"gulp": "^3.8.8",
40-
"gulp-bump": "~0.1.0",
38+
"chai": "^1.10.0",
39+
"gulp": "^3.9.0",
40+
"gulp-bump": "^0.1.13",
4141
"gulp-karma": "0.0.4",
42-
"gulp-ng-annotate": "^0.3.3",
42+
"gulp-ng-annotate": "^0.3.6",
4343
"gulp-sequence": "^0.3.1",
44-
"gulp-uglifyjs": "^0.4.3",
44+
"gulp-uglifyjs": "^0.4.4",
4545
"gulp-wrap": "^0.3.0",
46-
"karma": "^0.12.21",
47-
"karma-chai-sinon": "^0.1.4",
48-
"karma-chrome-launcher": "~0.1.0",
46+
"karma": "^0.12.37",
47+
"karma-chai-sinon": "^0.1.5",
48+
"karma-chrome-launcher": "^0.1.12",
4949
"karma-firefox-launcher": "~0.1.0",
5050
"karma-mocha": "~0.1",
51-
"karma-phantomjs-launcher": "~0.1.0",
51+
"karma-phantomjs-launcher": "^0.1.4",
5252
"karma-script-launcher": "~0.1.0",
53-
"sinon": "^1.12.1",
54-
"sinon-chai": "^2.6.0"
53+
"sinon": "^1.16.1",
54+
"sinon-chai": "^2.8.0"
5555
}
5656
}

src/angular-validation-match.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@ function match ($parse) {
1818

1919
var matchGetter = $parse(attrs.match);
2020
var caselessGetter = $parse(attrs.matchCaseless);
21+
var noMatchGetter = $parse(attrs.notMatch);
2122

2223
scope.$watch(getMatchValue, function(){
2324
ctrl.$$parseAndValidate();
2425
});
2526

2627
ctrl.$validators.match = function(){
2728
var match = getMatchValue();
28-
if(caselessGetter(scope) && angular.isString(match) && angular.isString(ctrl.$viewValue)){
29-
return ctrl.$viewValue.toLowerCase() === match.toLowerCase();
29+
var notMatch = noMatchGetter(scope);
30+
var value;
31+
32+
if(caselessGetter(scope)){
33+
value = angular.lowercase(ctrl.$viewValue) === angular.lowercase(match);
34+
}else{
35+
value = ctrl.$viewValue === match;
3036
}
31-
return ctrl.$viewValue === match;
37+
value ^= notMatch;
38+
return !!value;
3239
};
3340

3441
function getMatchValue(){

test/angular-input-match.spec.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,172 @@ describe('Directives: validation - match', function() {
180180
expect(form.testConfirm.$error.match).to.be.undefined();
181181
});
182182

183+
it('should check if $viewValues are not identical', function() {
184+
form.testConfirm.$setViewValue(inputValue+'extra');
185+
$scope.$digest();
186+
expect(form.testConfirm.$error.match).to.be.true();
187+
});
188+
189+
});
190+
191+
});
192+
193+
describe('not-match validation', function() {
194+
195+
describe('behavior:', function() {
196+
var validTemplate = '<input ng-model="confirmation" match="original" not-match="true"></input>';
197+
198+
it('returns true if no model value has been defined', function() {
199+
compiled = $compile(validTemplate)($scope);
200+
expect($scope.confirmation).to.be.undefined();
201+
$scope.$digest();
202+
expect(compiled.hasClass('ng-valid')).to.be.false();
203+
});
204+
205+
206+
it('returns true if $modelValue are identical', function() {
207+
$scope.confirmation = "value";
208+
compiled = $compile(validTemplate)($scope);
209+
$scope.original = "value";
210+
$scope.$digest();
211+
expect(compiled.hasClass('ng-valid')).to.be.false();
212+
});
213+
214+
it('returns true if $modelValue are not identical but match caselessly', function() {
215+
$scope.confirmation = "VALUE";
216+
compiled = $compile(validTemplate)($scope);
217+
$scope.original = "value";
218+
$scope.$digest();
219+
expect(compiled.hasClass('ng-valid')).to.be.true();
220+
});
221+
222+
it('returns false if $modelValue are not identical', function() {
223+
$scope.confirmation = false;
224+
$scope.original = undefined;
225+
compiled = $compile(validTemplate)($scope);
226+
$scope.$digest();
227+
expect(compiled.hasClass('ng-valid')).to.be.true();
228+
});
229+
230+
});
231+
232+
233+
describe('Form level validation', function() {
234+
var form,
235+
element,
236+
inputValue = 'testValue';
237+
238+
beforeEach(function() {
239+
element = angular.element(
240+
'<form name="form">' +
241+
'<input type="text" ng-model="test" name="test"></input>' +
242+
'<input type="text" match="form.test" not-match="true" ng-model="testConfirm" name="testConfirm"></input>' +
243+
'</form>'
244+
);
245+
$scope.test = inputValue;
246+
$compile(element)($scope);
247+
$scope.$digest();
248+
form = $scope.form;
249+
});
250+
251+
it('should check if $viewValues are identical', function() {
252+
form.testConfirm.$setViewValue(inputValue);
253+
$scope.$digest();
254+
expect(form.testConfirm.$error.match).to.be.true();
255+
});
256+
257+
it('should check if $viewValues are not identical', function() {
258+
form.testConfirm.$setViewValue(inputValue.toUpperCase());
259+
$scope.$digest();
260+
expect(form.testConfirm.$error.match).to.be.undefined();
261+
});
262+
263+
it('should check if $viewValues are not identical', function() {
264+
form.testConfirm.$setViewValue(inputValue+'extra');
265+
$scope.$digest();
266+
expect(form.testConfirm.$error.match).to.be.undefined();
267+
});
268+
269+
});
270+
271+
});
272+
273+
describe('not-match with caseless validation', function() {
274+
275+
describe('behavior:', function() {
276+
var validTemplate = '<input ng-model="confirmation" match="original" match-caseless="true" not-match="true"></input>';
277+
278+
it('returns true if no model value has been defined', function() {
279+
compiled = $compile(validTemplate)($scope);
280+
expect($scope.confirmation).to.be.undefined();
281+
$scope.$digest();
282+
expect(compiled.hasClass('ng-valid')).to.be.false();
283+
});
284+
285+
286+
it('returns true if $modelValue are identical', function() {
287+
$scope.confirmation = "value";
288+
compiled = $compile(validTemplate)($scope);
289+
$scope.original = "value";
290+
$scope.$digest();
291+
expect(compiled.hasClass('ng-valid')).to.be.false();
292+
});
293+
294+
it('returns true if $modelValue are not identical but match caselessly', function() {
295+
$scope.confirmation = "VALUE";
296+
compiled = $compile(validTemplate)($scope);
297+
$scope.original = "value";
298+
$scope.$digest();
299+
expect(compiled.hasClass('ng-valid')).to.be.false();
300+
});
301+
302+
it('returns false if $modelValue are not identical', function() {
303+
$scope.confirmation = false;
304+
$scope.original = undefined;
305+
compiled = $compile(validTemplate)($scope);
306+
$scope.$digest();
307+
expect(compiled.hasClass('ng-valid')).to.be.true();
308+
});
309+
310+
});
311+
312+
313+
describe('Form level validation', function() {
314+
var form,
315+
element,
316+
inputValue = 'testValue';
317+
318+
beforeEach(function() {
319+
element = angular.element(
320+
'<form name="form">' +
321+
'<input type="text" ng-model="test" name="test"></input>' +
322+
'<input type="text" match="form.test" match-caseless="true" not-match="true" ng-model="testConfirm" name="testConfirm"></input>' +
323+
'</form>'
324+
);
325+
$scope.test = inputValue;
326+
$compile(element)($scope);
327+
$scope.$digest();
328+
form = $scope.form;
329+
});
330+
331+
it('should check if $viewValues are identical', function() {
332+
form.testConfirm.$setViewValue(inputValue);
333+
$scope.$digest();
334+
expect(form.testConfirm.$error.match).to.be.true();
335+
});
336+
337+
it('should check if $viewValues are not identical but match caselessly', function() {
338+
form.testConfirm.$setViewValue(inputValue.toUpperCase());
339+
$scope.$digest();
340+
expect(form.testConfirm.$error.match).to.be.true();
341+
});
342+
343+
it('should check if $viewValues are not identical', function() {
344+
form.testConfirm.$setViewValue(inputValue+'extra');
345+
$scope.$digest();
346+
expect(form.testConfirm.$error.match).to.be.undefined();
347+
});
348+
183349
});
184350

185351
});

0 commit comments

Comments
 (0)