|
1 | | -angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSelect', function(sfValidator, sfSelect) { |
2 | | - return { |
3 | | - restrict: 'A', |
4 | | - scope: false, |
5 | | - // We want the link function to be *after* the input directives link function so we get access |
6 | | - // the parsed value, ex. a number instead of a string |
7 | | - priority: 500, |
8 | | - require: 'ngModel', |
9 | | - link: function(scope, element, attrs, ngModel) { |
10 | | - |
11 | | - |
12 | | - // We need the ngModelController on several places, |
13 | | - // most notably for errors. |
14 | | - // So we emit it up to the decorator directive so it can put it on scope. |
15 | | - scope.$emit('schemaFormPropagateNgModelController', ngModel); |
16 | | - |
17 | | - var error = null; |
18 | | - |
19 | | - var getForm = function() { |
20 | | - if (!form) { |
21 | | - form = scope.$eval(attrs.schemaValidate); |
22 | | - } |
23 | | - return form; |
24 | | - }; |
25 | | - var form = getForm(); |
26 | | - if (form.copyValueTo) { |
27 | | - ngModel.$viewChangeListeners.push(function() { |
28 | | - var paths = form.copyValueTo; |
29 | | - angular.forEach(paths, function(path) { |
30 | | - sfSelect(path, scope.model, ngModel.$modelValue); |
| 1 | +angular.module('schemaForm').directive('schemaValidate', ['sfValidator', '$parse', |
| 2 | + function(sfValidator, $parse) { |
| 3 | + |
| 4 | + return { |
| 5 | + restrict: 'A', |
| 6 | + scope: false, |
| 7 | + // We want the link function to be *after* the input directives link function so we get access |
| 8 | + // the parsed value, ex. a number instead of a string |
| 9 | + priority: 500, |
| 10 | + require: 'ngModel', |
| 11 | + link: function(scope, element, attrs, ngModel) { |
| 12 | + |
| 13 | + // We need the ngModelController on several places, |
| 14 | + // most notably for errors. |
| 15 | + // So we emit it up to the decorator directive so it can put it on scope. |
| 16 | + scope.$emit('schemaFormPropagateNgModelController', ngModel); |
| 17 | + |
| 18 | + var error = null; |
| 19 | + |
| 20 | + var getForm = function() { |
| 21 | + if (!form) { |
| 22 | + form = scope.$eval(attrs.schemaValidate); |
| 23 | + } |
| 24 | + return form; |
| 25 | + }; |
| 26 | + var form = getForm(); |
| 27 | + if (form.copyValueTo) { |
| 28 | + ngModel.$viewChangeListeners.push(function() { |
| 29 | + var paths = form.copyValueTo; |
| 30 | + angular.forEach(paths, function(path) { |
| 31 | + sfSelect(path, scope.model, ngModel.$modelValue); |
| 32 | + }); |
31 | 33 | }); |
32 | | - }); |
33 | | - } |
| 34 | + } |
34 | 35 |
|
35 | | - // Validate against the schema. |
| 36 | + // Validate against the schema. |
36 | 37 |
|
37 | | - var validate = function(viewValue) { |
38 | | - form = getForm(); |
39 | | - //Still might be undefined |
40 | | - if (!form) { |
41 | | - return viewValue; |
42 | | - } |
| 38 | + var validate = function(viewValue) { |
| 39 | + form = getForm(); |
| 40 | + //Still might be undefined |
| 41 | + if (!form) { |
| 42 | + return viewValue; |
| 43 | + } |
43 | 44 |
|
44 | | - // Omit TV4 validation |
45 | | - if (scope.options && scope.options.tv4Validation === false) { |
46 | | - return viewValue; |
47 | | - } |
| 45 | + // Omit TV4 validation |
| 46 | + if (scope.options && scope.options.tv4Validation === false) { |
| 47 | + return viewValue; |
| 48 | + } |
48 | 49 |
|
49 | | - var result = sfValidator.validate(form, viewValue); |
50 | | - // Since we might have different tv4 errors we must clear all |
51 | | - // errors that start with tv4- |
52 | | - Object.keys(ngModel.$error) |
| 50 | + var result = sfValidator.validate(form, viewValue); |
| 51 | + // Since we might have different tv4 errors we must clear all |
| 52 | + // errors that start with tv4- |
| 53 | + Object.keys(ngModel.$error) |
53 | 54 | .filter(function(k) { return k.indexOf('tv4-') === 0; }) |
54 | 55 | .forEach(function(k) { ngModel.$setValidity(k, true); }); |
55 | 56 |
|
56 | | - if (!result.valid) { |
57 | | - // it is invalid, return undefined (no model update) |
58 | | - ngModel.$setValidity('tv4-' + result.error.code, false); |
59 | | - error = result.error; |
60 | | - return undefined; |
| 57 | + if (!result.valid) { |
| 58 | + // it is invalid, return undefined (no model update) |
| 59 | + ngModel.$setValidity('tv4-' + result.error.code, false); |
| 60 | + error = result.error; |
| 61 | + return undefined; |
| 62 | + } |
| 63 | + return viewValue; |
| 64 | + }; |
| 65 | + |
| 66 | + // Custom validators, parsers, formatters etc |
| 67 | + if (typeof form.ngModel === 'function') { |
| 68 | + form.ngModel(ngModel); |
61 | 69 | } |
62 | | - return viewValue; |
63 | | - }; |
64 | 70 |
|
65 | | - // Custom validators, parsers, formatters etc |
66 | | - if (typeof form.ngModel === 'function') { |
67 | | - form.ngModel(ngModel); |
68 | | - } |
| 71 | + ['$parsers', '$viewChangeListeners', '$formatters'].forEach(function(attr) { |
| 72 | + if (form[attr] && ngModel[attr]) { |
| 73 | + form[attr].forEach(function(fn) { |
| 74 | + ngModel[attr].push(fn); |
| 75 | + }); |
| 76 | + } |
| 77 | + }); |
69 | 78 |
|
70 | | - ['$parsers', '$viewChangeListeners', '$formatters'].forEach(function(attr) { |
71 | | - if (form[attr] && ngModel[attr]) { |
72 | | - form[attr].forEach(function(fn) { |
73 | | - ngModel[attr].push(fn); |
74 | | - }); |
75 | | - } |
76 | | - }); |
| 79 | + ['$validators', '$asyncValidators'].forEach(function(attr) { |
| 80 | + // Check if our version of angular has i, i.e. 1.3+ |
| 81 | + if (form[attr] && ngModel[attr]) { |
| 82 | + angular.forEach(form[attr], function(fn, name) { |
| 83 | + ngModel[attr][name] = fn; |
| 84 | + }); |
| 85 | + } |
| 86 | + }); |
77 | 87 |
|
78 | | - ['$validators', '$asyncValidators'].forEach(function(attr) { |
79 | | - // Check if our version of angular has i, i.e. 1.3+ |
80 | | - if (form[attr] && ngModel[attr]) { |
81 | | - angular.forEach(form[attr], function(fn, name) { |
82 | | - ngModel[attr][name] = fn; |
83 | | - }); |
84 | | - } |
85 | | - }); |
86 | | - |
87 | | - // Get in last of the parses so the parsed value has the correct type. |
88 | | - // We don't use $validators since we like to set different errors depeding tv4 error codes |
89 | | - ngModel.$parsers.push(validate); |
90 | | - |
91 | | - // Listen to an event so we can validate the input on request |
92 | | - scope.$on('schemaFormValidate', function() { |
93 | | - if (ngModel.$setDirty) { |
94 | | - // Angular 1.3+ |
95 | | - ngModel.$setDirty(); |
96 | | - validate(ngModel.$modelValue); |
97 | | - } else { |
98 | | - // Angular 1.2 |
99 | | - ngModel.$setViewValue(ngModel.$viewValue); |
100 | | - } |
| 88 | + // Get in last of the parses so the parsed value has the correct type. |
| 89 | + // We don't use $validators since we like to set different errors depeding tv4 error codes |
| 90 | + ngModel.$parsers.push(validate); |
| 91 | + |
| 92 | + // Listen to an event so we can validate the input on request |
| 93 | + scope.$on('schemaFormValidate', function() { |
| 94 | + if (ngModel.$setDirty) { |
| 95 | + // Angular 1.3+ |
| 96 | + ngModel.$setDirty(); |
| 97 | + validate(ngModel.$modelValue); |
| 98 | + } else { |
| 99 | + // Angular 1.2 |
| 100 | + ngModel.$setViewValue(ngModel.$viewValue); |
| 101 | + } |
101 | 102 |
|
102 | | - }); |
| 103 | + }); |
103 | 104 |
|
104 | | - scope.schemaError = function() { |
105 | | - return error; |
106 | | - }; |
| 105 | + scope.schemaError = function() { |
| 106 | + return error; |
| 107 | + }; |
107 | 108 |
|
108 | | - } |
109 | | - }; |
110 | | -}]); |
| 109 | + } |
| 110 | + }; |
| 111 | + }]); |
0 commit comments