Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding TrevDev's new valid-expression validation directive #197

Closed
wants to merge 1 commit into from

2 participants

@ProLoser
Owner

WIP! Do not merge, this is for review and consideration.

Note that it may be worth while to just add support for a watch property to ui-validate instead of implementing this. Perhaps a second directive called ui-validate-watch that depends on ui-validate and imports the controller?

Theoretically, the 'expression' could be the functions passed to ui-validate.

@ProLoser
Owner

Will probably close this in lui of #198

@ProLoser ProLoser referenced this pull request
Closed

v0.3.1 patch #248

@ajoslin
Owner

I like #198 better too; the syntax is a little less confusing (watch being a seperate attribute)

@ProLoser ProLoser closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
0  modules/directives/validexp/test/validexpSpec.js
No changes.
View
39 modules/directives/validexp/validexp.js
@@ -0,0 +1,39 @@
+/**
+ * Validation Expressions with support for watch
+ * (might get merged into ui-validate)
+ *
+ * @author TrevDev
+ * @type {String}
+ * @example ui-validexp="{ exp:'$value > valA', watch: 'valA' }"
+ */
+angular.module('ui.directives').directive('uiValidexp', function () {
+ return {
+ restrict: 'A',
+ require: 'ngModel',
+ link: function (scope, elm, attrs, ctrl) {
+
+ var myObj = scope.$eval(attrs.uiValidexp);
+
+ var oldValue;
+ var validateExpValidator = function (value) {
+ oldValue = value;
+ if (scope.$eval(myObj.exp, { '$value': value })) {
+ ctrl.$setValidity('validexp', true);
+ return value;
+ } else {
+ ctrl.$setValidity('validexp', false);
+ return undefined;
+ }
+ };
+
+ ctrl.$parsers.push(validateExpValidator);
+ ctrl.$formatters.push(validateExpValidator);
+
+ if (myObj.watch) {
+ scope.$watch(myObj.watch, function () {
+ validateExpValidator(oldValue);
+ });
+ }
+ }
+ };
+});
Something went wrong with that request. Please try again.