Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit 85806bd

Browse files
Disorrderlukepfeiffer10
authored andcommitted
Add unsafe option (#149)
* Add `allowInvalidValue` option * Add test of 'allowInvalidValue'
1 parent 1a2aa11 commit 85806bd

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Inside of `ui-options`, you can customize these five properties:
6767
* `eventsToHandle` - default: `['input', 'keyup', 'click', 'focus']`
6868
* `addDefaultPlaceholder` - default: `true`
6969
* `escChar` - default: `'\\'`
70+
* `unsafe` - default: `false`
7071

7172
When customizing `eventsToHandle`, `clearOnBlur`, or `addDefaultPlaceholder`, the value you supply will replace the default. To customize `eventsToHandle`, be sure to replace the entire array.
7273

@@ -76,6 +77,8 @@ When setting `clearOnBlurPlaceholder` to `true`, it will show the placeholder te
7677

7778
If the `escChar` (\\ by default) is encountered in a mask, the next character will be treated as a literal and not a mask definition key. To disable the `escChar` feature completely, set `escChar` to `null`.
7879

80+
When `unsafe` set to true, apply value to `$modelValue` even if it isn't valid. By default, if you write invalid value, model stay `undefined`.
81+
7982
#### Global customization
8083
In addition to customizing behaviors for a specific element, you can also customize the behaviors globally. To do this, simply use the `uiMaskConfig` provider in your app configuration. Example:
8184

src/mask.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ angular.module('ui.mask', [])
3232
this.addDefaultPlaceholder = function(addDefaultPlaceholder) {
3333
return options.addDefaultPlaceholder = addDefaultPlaceholder;
3434
};
35+
this.allowInvalidValue = function(allowInvalidValue) {
36+
return options.allowInvalidValue = allowInvalidValue;
37+
};
3538
this.$get = ['uiMaskConfig', function(uiMaskConfig) {
3639
var tempOptions = uiMaskConfig;
3740
for(var prop in options) {
@@ -122,14 +125,27 @@ angular.module('ui.mask', [])
122125
}
123126
});
124127

128+
iAttrs.$observe('allowInvalidValue', function(val) {
129+
linkOptions.allowInvalidValue = val === ''
130+
? true
131+
: !!val;
132+
formatter(controller.$modelValue);
133+
});
134+
125135
function formatter(fromModelValue) {
126136
if (!maskProcessed) {
127137
return fromModelValue;
128138
}
129139
value = unmaskValue(fromModelValue || '');
130140
isValid = validateValue(value);
131141
controller.$setValidity('mask', isValid);
132-
return isValid && value.length ? maskValue(value) : undefined;
142+
143+
if (!value.length) return undefined;
144+
if (isValid || linkOptions.allowInvalidValue) {
145+
return maskValue(value);
146+
} else {
147+
return undefined;
148+
}
133149
}
134150

135151
function parser(fromViewValue) {
@@ -144,8 +160,11 @@ angular.module('ui.mask', [])
144160
// to be out-of-sync with what the controller's $viewValue is set to.
145161
controller.$viewValue = value.length ? maskValue(value) : '';
146162
controller.$setValidity('mask', isValid);
147-
if (isValid) {
148-
return modelViewValue ? controller.$viewValue : value;
163+
164+
if (modelViewValue) value = controller.$viewValue;
165+
if (!value.length) return undefined;
166+
if (isValid || linkOptions.allowInvalidValue) {
167+
return value;
149168
} else {
150169
return undefined;
151170
}

test/maskSpec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,22 @@ describe("uiMask", function () {
280280
input.val("811").triggerHandler("input");
281281
expect(input.val()).toBe("81.1_.__-___.__");
282282
});
283+
284+
it("should set the model value properly even if it's not full", function() {
285+
var input1 = compileElement('<input ui-mask="{{mask}}" ng-model="x" allow-invalid-value/>');
286+
var input2 = compileElement('<input ui-mask="{{mask}}" ng-model="x" ui-options="{allowInvalidValue: true}"/>');
287+
scope.$apply("mask = '9999'");
288+
289+
input1.val('11').triggerHandler("change");
290+
expect(scope.x).toBe("11");
291+
292+
input2.val('22').triggerHandler("change");
293+
expect(scope.x).toBe("22");
294+
295+
scope.$apply("x = '33'");
296+
expect(input1.val()).toBe("33__");
297+
expect(input2.val()).toBe("33__");
298+
});
283299
});
284300

285301
describe("verify change is called", function () {

0 commit comments

Comments
 (0)