Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#113 removing directive scope dependency #162

Merged
merged 4 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@
],
"description": "Client-side Validation for AngularJS",
"main": "dist/angular-validation.js",
"keywords": ["angular", "angularjs", "validation", "angular validation", "validator", "client-side"],
"keywords": [
"angular",
"angularjs",
"validation",
"angular validation",
"validator",
"client-side"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
],
"dependencies": {
"bootstrap": "~3.3.6",
"ui-bootstrap": "~0.14.3"
}
}
16 changes: 15 additions & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function() {
angular.module('myApp', ['validation', 'validation.rule'])
angular.module('myApp', ['validation', 'validation.rule', 'ui.bootstrap', 'ui.bootstrap.tpls'])

// -------------------
// config phase
Expand Down Expand Up @@ -150,6 +150,20 @@
checkValid: $validationProvider.checkValid
};

$scope.form7 = {
dt: new Date(),
open: function($event) {
$scope.form7.status.opened = true;
},
dateOptions: {
formatYear: 'yy',
startingDay: 1
},
status: {
opened: false
}
};

// Callback method
$scope.success = function(message) {
alert('Success ' + message);
Expand Down
47 changes: 22 additions & 25 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@
var $validationProvider = $injector.get('$validation');
var $q = $injector.get('$q');
var $timeout = $injector.get('$timeout');
var $parse = $injector.get('$parse');

/**
* Do this function if validation valid
Expand All @@ -341,11 +342,12 @@
* @param ctrl
* @returns {}
*/
var validFunc = function(element, validMessage, validation, scope, ctrl) {
var validFunc = function(element, validMessage, validation, scope, ctrl, attrs) {
var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).success;
var validCallback = $parse('success');
var messageElem;

if (scope.messageId) messageElem = angular.element(document.querySelector('#' + scope.messageId));
if (attrs.messageId) messageElem = angular.element(document.querySelector('#' + attrs.messageId));
else messageElem = element.next();

if (element.attr('no-validation-message')) {
Expand All @@ -358,7 +360,7 @@
}

ctrl.$setValidity(ctrl.$name, true);
if (scope.validCallback) scope.validCallback({
if (validCallback) validCallback({
message: messageToShow
});
if ($validationProvider.validCallback) $validationProvider.validCallback(element);
Expand All @@ -376,11 +378,12 @@
* @param ctrl
* @returns {}
*/
var invalidFunc = function(element, validMessage, validation, scope, ctrl) {
var invalidFunc = function(element, validMessage, validation, scope, ctrl, attrs) {
var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).error;
var invalidCallback = $parse('error');
var messageElem;

if (scope.messageId) messageElem = angular.element(document.querySelector('#' + scope.messageId));
if (attrs.messageId) messageElem = angular.element(document.querySelector('#' + attrs.messageId));
else messageElem = element.next();

if (element.attr('no-validation-message')) {
Expand All @@ -393,7 +396,7 @@
}

ctrl.$setValidity(ctrl.$name, false);
if (scope.invalidCallback) scope.invalidCallback({
if (invalidCallback) invalidCallback({
message: messageToShow
});
if ($validationProvider.invalidCallback) $validationProvider.invalidCallback(element);
Expand Down Expand Up @@ -432,15 +435,15 @@
var expression = $validationProvider.getExpression(validator);
var valid = {
success: function() {
validFunc(element, attrs[successMessage], validator, scope, ctrl);
validFunc(element, attrs[successMessage], validator, scope, ctrl, attrs);
if (leftValidation.length) {
return checkValidation(scope, element, attrs, ctrl, leftValidation, value);
} else {
return true;
}
},
error: function() {
return invalidFunc(element, attrs[errorMessage], validator, scope, ctrl);
return invalidFunc(element, attrs[errorMessage], validator, scope, ctrl, attrs);
}
};

Expand Down Expand Up @@ -483,13 +486,6 @@
return {
restrict: 'A',
require: 'ngModel',
scope: {
model: '=ngModel',
initialValidity: '=initialValidity',
validCallback: '&',
invalidCallback: '&',
messageId: '@'
},
link: function(scope, element, attrs, ctrl) {
/**
* watch
Expand Down Expand Up @@ -526,7 +522,7 @@
/**
* Default Valid/Invalid Message
*/
if (!scope.messageId) element.after('<span></span>');
if (!attrs.messageId) element.after('<span></span>');

/**
* Set custom initial validity
Expand All @@ -551,7 +547,7 @@
ctrl.$setPristine();
ctrl.$setValidity(ctrl.$name, undefined);
ctrl.$render();
if (scope.messageId) angular.element(document.querySelector('#' + scope.messageId)).html('');
if (attrs.messageId) angular.element(document.querySelector('#' + attrs.messageId)).html('');
else element.next().html('');
});
});
Expand All @@ -572,9 +568,9 @@
if (attrs.validMethod === 'submit') {
// clear previous scope.$watch
watch();
watch = scope.$watch('model', function(value, oldValue) {
value = ctrl.$viewValue;

watch = scope.$watch(function() {
return scope.$eval(attrs.ngModel);
}, function(value, oldValue) {
// don't watch when init
if (value === oldValue) {
return;
Expand Down Expand Up @@ -612,7 +608,7 @@
*/
if (attrs.validMethod === 'blur') {
element.bind('blur', function() {
var value = ctrl.$viewValue;
var value = scope.$eval(attrs.ngModel);
scope.$apply(function() {
checkValidation(scope, element, attrs, ctrl, validation, value);
});
Expand All @@ -632,8 +628,9 @@
* Validate watch method
* This is the default method
*/
scope.$watch('model', function(value) {
value = ctrl.$viewValue;
scope.$watch(function() {
return scope.$eval(attrs.ngModel);
}, function(value) {
/**
* dirty, pristine, viewValue control here
*/
Expand All @@ -642,7 +639,7 @@
ctrl.$setViewValue(ctrl.$viewValue);
} else if (ctrl.$pristine) {
// Don't validate form when the input is clean(pristine)
if (scope.messageId) angular.element(document.querySelector('#' + scope.messageId)).html('');
if (attrs.messageId) angular.element(document.querySelector('#' + attrs.messageId)).html('');
else element.next().html('');
return;
}
Expand All @@ -655,7 +652,7 @@
*/
attrs.$observe('noValidationMessage', function(value) {
var el;
if (scope.messageId) el = angular.element(document.querySelector('#' + scope.messageId));
if (attrs.messageId) el = angular.element(document.querySelector('#' + attrs.messageId));
else el = element.next();
if (value === 'true' || value === true) el.css('display', 'none');
else if (value === 'false' || value === false) el.css('display', 'block');
Expand Down
Loading