Skip to content

Commit 535114c

Browse files
committed
add validator tests
1 parent fcd6983 commit 535114c

File tree

6 files changed

+35
-23
lines changed

6 files changed

+35
-23
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ app.config(['FormusValidatorProvider', function (FormusValidatorProvider) {
132132
});
133133
}]);
134134
```
135-
Validator function can take three parameters:
136-
- field value
137-
- field config
138-
- validator options
135+
Validator function is called by $injector and has three special parameters:
136+
- `value` - field value
137+
- `config` - field config
138+
- `args` - validator options
139139
140140
This validator can take options as object with property `min`, e.g:
141141
```js

app/js/formus.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ formus.directive('formusField', function($injector, $http, $compile, $log, $temp
179179
}
180180
};
181181

182-
$scope.$on('Formus.validate', function() {
182+
$scope.$on('Formus.validate', function(e) {
183183
if (!$scope.config.hide) {
184184
$scope.validation($scope.model);
185185
} else {
@@ -784,7 +784,7 @@ formus.provider('FormusValidator', function($logProvider) {
784784
return null;
785785
}
786786
};
787-
var getProvider = function($log) {
787+
var getProvider = function($log, $injector) {
788788
function get(name) {
789789
if (validators[name]) {
790790
return validators[name];
@@ -793,7 +793,11 @@ formus.provider('FormusValidator', function($logProvider) {
793793

794794
function validate(validatorName, value, config, args) {
795795
if (validators[validatorName]) {
796-
return validators[validatorName](value, config, args);
796+
return $injector.invoke(validators[validatorName], null, {
797+
value: value,
798+
config: config,
799+
args: args
800+
});
797801
}
798802
$log.warn('Don\'t find validator with name "' + validatorName + '"');
799803
return null;

dist/formus.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
$scope.isValid = true;
160160
$scope.dirty = false;
161161
$scope.validation = function (value) {
162-
if (_.isObject($scope.config.validators)) {
162+
if (angular.isObject($scope.config.validators)) {
163163
$scope.errors = [];
164164
angular.forEach($scope.config.validators, function (args, name) {
165165
var error = FormusValidator.validate(name, value, $scope.config, args);
@@ -169,7 +169,7 @@
169169
});
170170
}
171171
};
172-
$scope.$on('Formus.validate', function () {
172+
$scope.$on('Formus.validate', function (e) {
173173
if (!$scope.config.hide) {
174174
$scope.validation($scope.model);
175175
} else {
@@ -180,7 +180,7 @@
180180
});
181181
var init = function () {
182182
$scope.parentCtrl = $element.parent().controller('formus-field');
183-
if (_.isUndefined($scope.parentCtrl)) {
183+
if (angular.isUndefined($scope.parentCtrl)) {
184184
$scope.parentCtrl = $element.parent().controller('formus-form');
185185
}
186186
$scope.parentScope = $scope.parentCtrl.getScope();
@@ -203,7 +203,7 @@
203203
$scope.parentScope.errors = FormusHelper.setNested($scope.parentScope.errors, $scope.config.name, newValue);
204204
}
205205
});
206-
$scope.isParent = !_.isUndefined($scope.config.fields);
206+
$scope.isParent = !angular.isUndefined($scope.config.fields);
207207
/** Set field type 'fieldset' when it has child fields and don't set other type */
208208
if ($scope.isParent && _.isUndefined($scope.config.input)) {
209209
$scope.config.input = 'fieldset';
@@ -213,7 +213,7 @@
213213
$element: $element,
214214
$attr: $attr
215215
});
216-
if (_.isFunction($scope.config.linker)) {
216+
if (angular.isFunction($scope.config.linker)) {
217217
$injector.invoke($scope.config.linker, this, {
218218
$scope: $scope,
219219
$element: $element,
@@ -312,7 +312,7 @@
312312
};
313313
$scope.submit = function () {
314314
$scope.validate().then(function () {
315-
if (typeof $scope.config.submit.handler === 'function') {
315+
if (angular.isFunction($scope.config.submit.handler)) {
316316
$scope.config.submit.handler();
317317
}
318318
}, angular.noop);
@@ -528,7 +528,7 @@
528528
$scope.setElementTemplate = function (templateData) {
529529
$element.html(templateData);
530530
$compile($element.contents())($scope);
531-
if (typeof $scope.afterLoadTemplate === 'function') {
531+
if (angular.isFunction($scope.afterLoadTemplate)) {
532532
$scope.afterLoadTemplate();
533533
}
534534
};
@@ -570,7 +570,7 @@
570570
$scope.config[name] = value;
571571
}
572572
});
573-
if ($scope.config.label && typeof $scope.config.showLabel === 'undefined') {
573+
if ($scope.config.label && angular.isUndefined($scope.config.showLabel)) {
574574
$scope.config.showLabel = true;
575575
}
576576
};
@@ -642,7 +642,7 @@
642642
* @param templateUrl
643643
*/
644644
var setTemplateUrl = function (name, templateUrl) {
645-
if (typeof name === 'string') {
645+
if (angular.isString(name)) {
646646
templateMap[name] = templateUrl;
647647
} else {
648648
angular.forEach(name, function (templateUrl, name) {
@@ -750,15 +750,19 @@
750750
return null;
751751
}
752752
};
753-
var getProvider = function ($log) {
753+
var getProvider = function ($log, $injector) {
754754
function get(name) {
755755
if (validators[name]) {
756756
return validators[name];
757757
}
758758
}
759759
function validate(validatorName, value, config, args) {
760760
if (validators[validatorName]) {
761-
return validators[validatorName](value, config, args);
761+
return $injector.invoke(validators[validatorName], null, {
762+
value: value,
763+
config: config,
764+
args: args
765+
});
762766
}
763767
$log.warn('Don\'t find validator with name "' + validatorName + '"');
764768
return null;
@@ -769,7 +773,7 @@
769773
};
770774
};
771775
var set = function (name, callback) {
772-
if (typeof callback === 'function') {
776+
if (angular.isFunction(callback)) {
773777
validators[name] = callback;
774778
} else {
775779
$logProvider.warn('Validator must be function. Can\'t set validator with name "' + name + '"');

0 commit comments

Comments
 (0)