Skip to content

Commit

Permalink
Merge pull request #70 from lvarayut/master
Browse files Browse the repository at this point in the history
Add a MessageId atrribute for customizing valid/invalid message position
  • Loading branch information
Huei Tan committed Feb 2, 2015
2 parents 744edde + a3841d9 commit 8a86b03
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 91 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ module.exports = function(grunt) {
grunt.registerTask('dev', ['browserSync', 'watch']);
grunt.registerTask('build', ['clean', 'concat', 'uglify']);
grunt.registerTask('check', ['jshint', 'jsbeautifier', 'build']); // use this before commit
grunt.registerTask('test', ['karma']);

};
2 changes: 1 addition & 1 deletion config/karma.conf.angular.1.3.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ module.exports = function(config) {
'karma-phantomjs-launcher'
]
});
};
};
Empty file modified dist/angular-validation-rule.js
100755 → 100644
Empty file.
Empty file modified dist/angular-validation-rule.min.js
100755 → 100644
Empty file.
64 changes: 45 additions & 19 deletions dist/angular-validation.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@
*/
this.showErrorMessage = true;


/**
* Check form valid, return true
* checkValid(Form): Check the specific form(Form) valid from angular `$valid`
Expand Down Expand Up @@ -277,7 +276,7 @@

});
}).call(this);


(function() {
angular.module('validation.directive', ['validation.provider'])
.directive('validator', ['$injector',
Expand All @@ -296,18 +295,23 @@
* @param ctrl
* @returns {}
*/
var validFunc = function(element, validMessage, validation, callback, ctrl) {
var messageElem = element.next(),
var validFunc = function(element, validMessage, validation, scope, ctrl) {
var messageElem,
messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).success;

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

if ($validationProvider.showSuccessMessage && messageToShow) {
messageElem.html($validationProvider.getSuccessHTML(messageToShow));
messageElem.css('display', '');
} else {
messageElem.css('display', 'none');
}
ctrl.$setValidity(ctrl.$name, true);
if (callback) callback();
if (scope.validCallback) scope.validCallback();

return true;
};
Expand All @@ -322,18 +326,23 @@
* @param ctrl
* @returns {}
*/
var invalidFunc = function(element, validMessage, validation, callback, ctrl) {
var messageElem = element.next(),
var invalidFunc = function(element, validMessage, validation, scope, ctrl) {
var messageElem,
messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).error;

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

if ($validationProvider.showErrorMessage && messageToShow) {
messageElem.html($validationProvider.getErrorHTML(messageToShow));
messageElem.css('display', '');
} else {
messageElem.css('display', 'none');
}
ctrl.$setValidity(ctrl.$name, false);
if (callback) callback();
if (scope.inValidCallback) scope.inValidCallback();

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

Expand All @@ -402,7 +411,11 @@
}
// Check with RegExp
else if (expression.constructor === RegExp) {
return $validationProvider.getExpression(validator).test(value) ? valid.success() : valid.error();
// Only apply the test if the value is neither undefined or null
if (value !== undefined && value !== null)
return $validationProvider.getExpression(validator).test(value) ? valid.success() : valid.error();
else
return valid.error();
} else {
return valid.error();
}
Expand All @@ -427,7 +440,8 @@
model: '=ngModel',
initialValidity: '=initialValidity',
validCallback: '&',
invalidCallback: '&'
invalidCallback: '&',
messageId: '@'
},
link: function(scope, element, attrs, ctrl) {

Expand All @@ -454,10 +468,6 @@
*/
var uid = ctrl.validationId = guid();

/**
* Valid/Invalid Message
*/
element.after('<span></span>');

/**
* Set initial validity to undefined if no boolean value is transmitted
Expand All @@ -467,6 +477,12 @@
initialValidity = scope.initialValidity;
}

/**
* Default Valid/Invalid Message
*/
if (!scope.messageId)
element.after('<span></span>');

/**
* Set custom initial validity
* Usage: <input initial-validity="true" ... >
Expand All @@ -491,7 +507,10 @@
ctrl.$setPristine();
ctrl.$setValidity(ctrl.$name, undefined);
ctrl.$render();
element.next().html('');
if (scope.messageId)
angular.element(document.querySelector('#' + scope.messageId)).html('');
else
element.next().html('');
});

/**
Expand Down Expand Up @@ -574,7 +593,10 @@
ctrl.$setViewValue(ctrl.$viewValue);
} else if (ctrl.$pristine) {
// Don't validate form when the input is clean(pristine)
element.next().html('');
if (scope.messageId)
angular.element(document.querySelector('#' + scope.messageId)).html('');
else
element.next().html('');
return;
}
checkValidation(scope, element, attrs, ctrl, validation, value);
Expand All @@ -587,7 +609,11 @@
* Don't showup the validation Message
*/
attrs.$observe('noValidationMessage', function(value) {
var el = element.next();
var el;
if (scope.messageId)
el = angular.element(document.querySelector('#' + scope.messageId));
else
el = element.next();
if (value == 'true' || value === true) {
el.css('display', 'none');
} else if (value == 'false' || value === false) {
Expand Down
Loading

0 comments on commit 8a86b03

Please sign in to comment.