Skip to content

Commit 6a1c698

Browse files
committed
Using $submitted of formController. close #3
1 parent b9ed261 commit 6a1c698

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

dist/bootstrap-angular-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directives/form.directive.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,11 @@ angular.module("bootstrap.angular.validation").directive("form", ["$parse", "$ro
5151
var preLinkFunction = function($scope, formElement, $attr, formController) {
5252
// Expose a method to manually trigger the validation
5353
formController.$validate = function() {
54-
$scope.formSubmissionAttempted = true;
54+
formController.$setSubmitted();
5555
};
5656

5757
formElement.on("submit", function(e) {
58-
$scope.$apply(function() {
59-
/*
60-
* Notify all "bs-validation" directive that user has tried submitting the form, now we can
61-
* display validation error messages (if any). This is required, since if we immediately
62-
* starts displaying the validation error messages as the form displayed or as the user starts
63-
* typing, that will not a good user experience.
64-
*/
65-
$scope.formSubmissionAttempted = true;
66-
});
67-
68-
// If any of the form element does not pass the validation
58+
// If any of the form element has not passed the validation
6959
if (formController.$invalid) {
7060
// Then focus the first invalid element
7161
formElement[0].querySelector(".ng-invalid").focus();
@@ -76,7 +66,7 @@ angular.module("bootstrap.angular.validation").directive("form", ["$parse", "$ro
7666
var submitHandler = $parse(ngSubmit);
7767
$scope.$apply(function() {
7868
submitHandler($scope, {$event: e});
79-
$scope.formSubmissionAttempted = false;
69+
formController.$setPristine();
8070
});
8171

8272
return true;

src/directives/validation.directive.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
angular.module("bootstrap.angular.validation").directive("bsValidation", ["$interpolate", "BsValidationService", function($interpolate, bsValidationService) {
2727
return {
2828
restrict: "A",
29-
require: "ngModel",
30-
link: function($scope, $element, $attr, ngModelController) {
29+
require: ["ngModel", "^^form"],
30+
link: function($scope, $element, $attr, controllers) {
31+
var ngModelController = controllers[0];
32+
var ngFormController = controllers[1];
33+
3134
var errorElementClass = "bs-invalid-msg";
3235
// All classed needed to add to validation message
3336
var errorClasses = [errorElementClass, "help-block"];
@@ -74,7 +77,7 @@ angular.module("bootstrap.angular.validation").directive("bsValidation", ["$inte
7477
* Do not show or hide error for current element don't have any validation errors or has validation
7578
* error but user has not attempted to submit the form yet.
7679
*/
77-
if (!$scope.formSubmissionAttempted || !ngModelController.$invalid) {
80+
if (!ngFormController.$submitted || !ngModelController.$invalid || ngModelController.$pristine) {
7881
formGroupElement.removeClass("has-error");
7982

8083
if (formGroupElement.length > 0) {
@@ -133,7 +136,7 @@ angular.module("bootstrap.angular.validation").directive("bsValidation", ["$inte
133136

134137
// Look when user try to submit the form & display validation errors.
135138
$scope.$watchCollection(function() {
136-
return $scope.formSubmissionAttempted && ngModelController.$error;
139+
return ngFormController.$submitted && ngModelController.$error;
137140
}, displayOrHideError);
138141
}
139142
};

0 commit comments

Comments
 (0)