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

Commit 1283a3a

Browse files
committed
Added $validationOptions to fix issue #25
Added $validationOptions to set debounce globally
1 parent 92b7ef4 commit 1283a3a

File tree

7 files changed

+64
-19
lines changed

7 files changed

+64
-19
lines changed

app.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ myApp.controller('Ctrl', ['$location', '$route', '$scope', '$translate', functio
4848
// -- Controller to use Angular-Validation Directive
4949
// -----------------------------------------------
5050
myApp.controller('CtrlValidationDirective', ['$scope', 'validationService', function ($scope, validationService) {
51+
$scope.$validationOptions = { debounce: 4500 };
52+
5153
$scope.submitForm = function() {
5254
if(new validationService().checkFormValidity($scope.form1)) {
5355
alert('All good, proceed with submit...');
5456
}
5557
}
5658
$scope.showValidationSummary = function () {
57-
$scope.displayValidationSummary = true;
59+
$translate('SHOW_VALIDATION_SUMMARY').then(function() {
60+
$scope.displayValidationSummary = true;
61+
});
5862
}
5963
}]);
6064

@@ -67,7 +71,9 @@ myApp.controller('Ctrl2forms', ['$scope', 'validationService', function ($scope,
6771
}
6872
}
6973
$scope.showValidationSummary = function () {
70-
$scope.displayValidationSummary = true;
74+
$translate('SHOW_VALIDATION_SUMMARY').then(function() {
75+
$scope.displayValidationSummary = true;
76+
});
7177
}
7278
}]);
7379

@@ -121,7 +127,9 @@ myApp.controller('CtrlValidationService', ['$scope', '$translate', 'validationSe
121127
};
122128

123129
$scope.showValidationSummary = function () {
124-
$scope.displayValidationSummary = true;
130+
$translate('SHOW_VALIDATION_SUMMARY').then(function() {
131+
$scope.displayValidationSummary = true;
132+
});
125133
}
126134

127135
$scope.submitForm = function() {

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ghiscoding.angular-validation",
3-
"version": "1.3.16",
3+
"version": "1.3.17",
44
"authors": [
55
"Ghislain B."
66
],

changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ Angular-Validation change logs
1717
1.3.13 (2015-04-06) Fixed $translate delay issue when using external JSON files
1818
1.3.14 (2015-04-07) Merge pull request #19 Added norwegian translation and changes to allow user to remove invalid validators.
1919
1.3.15 (2015-04-08) Fixed #23 If multiple forms exist in the app the errors in 1 form affect validation in the other
20-
1.3.16 (2015-04-09) Accept Merge #3 Fixed removeFromValidationSummary to also remove from 'local' array
20+
1.3.16 (2015-04-09) Accept Merge #3 Fixed removeFromValidationSummary to also remove from 'local' array
21+
1.3.17 (2015-04-11) Added global `$scope.$validationOptions` object, for now only has the `debounce` property that be used by both the Directive and Service.

dist/angular-validation.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ghiscoding.angular-validation",
3-
"version": "1.3.16",
3+
"version": "1.3.17",
44
"author": "Ghislain B.",
55
"description": "Angular-Validation Directive and Service (ghiscoding)",
66
"main": "app.js",

readme.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Angular Validation (Directive / Service)
2-
`Version: 1.3.16`
2+
`Version: 1.3.17`
33
### Form validation after user inactivity of default 1sec. (customizable timeout)
44

55
Forms Validation with Angular made easy! Angular-Validation is an angular directive/service with locales (languages) with a very simple approach of defining your `validation=""` directly within your element to validate (input, textarea, etc) and...that's it!!! The directive/service will take care of the rest!
@@ -16,6 +16,8 @@ Huge rewrite to have a better code separation and also adding support to Service
1616

1717
[Validation summary](#validation-summary) was also recently added to easily show all validation errors that are still active on the form and you can also use 2 ways of dealing with the [Submit](#submit) button.
1818

19+
For more explanations, see the question answered: [Why Use It?](#whyuseit)
20+
1921
<a name="plunker"></a>
2022
## Live Demo
2123
[Plunker](http://plnkr.co/jADq7H)
@@ -29,6 +31,7 @@ Huge rewrite to have a better code separation and also adding support to Service
2931
* [Demo - Plunker](#plunker)
3032
* [Dependencies](#dependencies)
3133
* [Form Submit and Validation](#submit)
34+
* [Global Options](#global-options)
3235
* [Install (bower)](#install)
3336
* [Include it in your app project](#project)
3437
* [Locales (languages)](#locales)
@@ -37,6 +40,27 @@ Huge rewrite to have a better code separation and also adding support to Service
3740
* [Some Working Examples (Directive)](#examples-directive)
3841
* [Some Working Examples (Service)](#examples-service)
3942
* [Validation summary](#validation-summary)
43+
* [Why Use It?](#whyuseit)
44+
45+
<a name="whyuseit"></a>
46+
Why use angular-validation?
47+
-----
48+
Angular-validation was develop with DRY (Don't Repeat Yourself) and simplicity in mind.
49+
You can transform this:
50+
```html
51+
<input type="text" name="username" ng-model="user.username"
52+
ng-minlength="3" ng-maxlength="8" required />
53+
<div ng-show="form.$submitted || form.user.$touched">
54+
<span ng-show="userForm.username.$error.minlength" class="help-block">Username is too short.</p>
55+
<span ng-show="userForm.username.$error.maxlength" class="help-block">Username is too long.</p>
56+
</div>
57+
```
58+
into this:
59+
```html
60+
<input type="text" name="username" ng-model="user.username"
61+
validation="min_len:3|max_len:8|required" />
62+
```
63+
The Angular-Validation will create by itself the necessary error message. Now imagine your form with 10 inputs, using the Angular-Validation will end up using 10 lines of code, while on the other hand using the default of Angular will give you 30 lines of code... so what are you waiting for? Use Angular-Validation!!! :)
4064

4165
<a name="install"></a>
4266
Install
@@ -98,6 +122,8 @@ Angular-Validation requires the element which will use validation to have an htm
98122
Let's start with a simple example and then let's get down to real business.
99123

100124
P.S. For real live sample, see the [live plunker demo](#plunker) or download the Github project and run the `index.html` (on the exception of Chrome who doesn't want to run http outside of webserver) while the actual form with validation is inside `templates/testingFormDirective.html` for a better separation.
125+
126+
*To define the debounce globally (for all form elements), you could use `$scope.$validationOptions = { debounce: 1500 };` or set it on each element `<input debounce="1500"/>*
101127
```html
102128
<!-- example 1 -->
103129
<!-- change the debounce or typing-limit (timer in ms of inactivity) after which will trigger the validation check -->
@@ -186,6 +212,15 @@ P.S. For real live sample, see the [live demo](#plunker) or download the Github
186212
};
187213

188214
```
215+
<a name="global-options"></a>
216+
## Global Options
217+
To change default options, you can change the `$scope.$validationOptions`, for now only the `debounce` property is used but this might expend in the future.
218+
To define the debounce globally (for all form elements), you could use `$scope.$validationOptions = { debounce: 1500 };` or set it on each element `<input debounce="1500"/>
219+
```javascript
220+
myApp.controller('Ctrl', function ($scope) {
221+
$scope.$validationOptions = { debounce: 1500 }; // set the debounce globally
222+
});
223+
```
189224

190225
<a name="submit"></a>
191226
## Form Submit and Validation
@@ -461,4 +496,5 @@ License
461496
* [1.3.13](https://github.com/ghiscoding/angular-validation/commit/d0440bdd7fc2816e03d28ad3a9c3bd7bee8ac519) `2015-04-06` Fixed $translate delay issue when using external JSON files
462497
* [1.3.14](https://github.com/ghiscoding/angular-validation/pull/19) `2015-04-07` Merge pull request #19 Added norwegian translation and changes to allow user to remove invalid validators
463498
* [1.3.15](https://github.com/ghiscoding/angular-validation/commit/24037e4b2e22658e7e2011c022ba4cca26f391d9) `2015-04-08` Fixed #23 If multiple forms exist in the app the errors in 1 form affect validation in the other
464-
* [1.3.16]() `2015-04-09` Accept Merge #3 Fixed removeFromValidationSummary to also remove from 'local' array
499+
* [1.3.16](https://github.com/ghiscoding/angular-validation/commit/6c419d45bdb00341416d91199003d827259bd5da) `2015-04-09` Accept Merge #3 Fixed removeFromValidationSummary to also remove from 'local' array
500+
* [1.3.17]() `2015-04-11` Added global `$scope.$validationOptions` [Global Options](#global-options) object, for now only has the `debounce` property that be used by both the Directive and Service.

src/validation-common.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
angular
1313
.module('ghiscoding.validation')
14-
.factory('validationCommon', ['$filter', '$timeout', '$translate', 'validationRules', function ($filter, $timeout, $translate, validationRules) {
14+
.factory('validationCommon', ['$q', '$timeout', '$translate', 'validationRules', function ($q, $timeout, $translate, validationRules) {
1515
// global variables of our object
1616
var bFieldRequired = false; // by default we'll consider our field not required, if validation attribute calls it, then we'll start validating
1717
var INACTIVITY_LIMIT = 1000; // constant of maximum user inactivity time limit, this is the default cosntant but can be variable through typingLimit variable
@@ -26,8 +26,6 @@ angular
2626
var validators = []; // Array of all Form Validators
2727
var validatorAttrs = {}; // Current Validator attributes
2828
var validationSummary = []; // Array Validation Error Summary
29-
var validationSummaries = {}; // Validation Error Summaries separated by Form (could be multiple forms)
30-
var $translate = $filter('translate');
3129

3230
// service constructor
3331
var validationCommon = function(scope, elm, attrs, ctrl) {
@@ -79,6 +77,8 @@ angular
7977
self.typingLimit = parseInt(self.validatorAttrs.debounce, 10);
8078
}else if(self.validatorAttrs.hasOwnProperty('typingLimit')) {
8179
self.typingLimit = parseInt(self.validatorAttrs.typingLimit, 10);
80+
}else if(!!self.scope.$validationOptions && self.scope.$validationOptions.hasOwnProperty('debounce')) {
81+
self.typingLimit = parseInt(self.scope.$validationOptions.debounce, 10);
8282
}
8383

8484
// We first need to see if the validation holds a custom user regex, if it does treat it first
@@ -204,7 +204,7 @@ angular
204204
}
205205

206206
// user might have passed a message to be translated
207-
var errorMsg = (!!attrs && !!attrs.translate) ? $translate(message) : message;
207+
var errorMsg = (!!attrs && !!attrs.translate) ? $translate.instant(message) : message;
208208

209209
// get the name attribute of current element, make sure to strip dirty characters, for example remove a <input name="options[]"/>, we need to strip the "[]"
210210
var elmInputName = elmName.replace(/[|&;$%@"<>()+,\[\]\{\}]/g, '');
@@ -308,7 +308,7 @@ angular
308308
} else {
309309
// before running Regex test, we'll make sure that an input of type="number" doesn't hold invalid keyboard chars, if true skip Regex
310310
if(typeof strValue === "string" && strValue === "" && self.elm.prop('type').toUpperCase() === "NUMBER") {
311-
message = $translate("INVALID_KEY_CHAR");
311+
message = $translate.instant("INVALID_KEY_CHAR");
312312
isValid = false;
313313
}else {
314314
// run the Regex test through each iteration, if required (\S+) and is null then it's invalid automatically
@@ -330,7 +330,7 @@ angular
330330
message += ' ';
331331
message += (!!self.validators[j].altText && self.validators[j].altText.length > 0)
332332
? self.validators[j].altText.replace("alt=", "")
333-
: $translate(self.validators[j].message);
333+
: $translate.instant(self.validators[j].message);
334334

335335
// replace any error message param(s) that were possibly passed
336336
if(typeof self.validators[j].params !== "undefined") {
@@ -349,13 +349,13 @@ angular
349349
message = message.trim();
350350

351351
// only log the invalid message in the $validationSummary
352-
addToValidationSummary(self, $translate(message));
352+
addToValidationSummary(self, $translate.instant(message));
353353

354354
// change the Form element object boolean flag from the `formElements` variable
355355
var formElmObj = getFormElementByName(self.elm.attr('name'));
356356
if(!!formElmObj) {
357357
formElmObj.isValid = isValid;
358-
formElmObj.message = $translate(message);
358+
formElmObj.message = $translate.instant(message);
359359
}
360360

361361
// error Display

0 commit comments

Comments
 (0)