|
7 | 7 | * element. |
8 | 8 | */ |
9 | 9 | angular.module('bootstrap.angular.validation').directive('form', [ |
10 | | - '$parse', 'BsValidationService', |
11 | | - function($parse, validationService) { |
| 10 | + '$parse', 'BsValidationService', function ($parse, validationService) { |
12 | 11 | return { |
13 | 12 | restrict: 'E', |
14 | 13 | require: 'form', |
15 | 14 | priority: 1000, // Setting a higher priority so that this directive compiles first. |
16 | | - compile: function($formElement, $formAttributes) { |
17 | | - if (validationService.isValidationDisabled($formElement)) { |
18 | | - return; |
19 | | - } |
| 15 | + compile: function ($formElement, $formAttributes) { |
| 16 | + if (validationService.isValidationDisabled($formElement)) { |
| 17 | + return; |
| 18 | + } |
20 | 19 |
|
21 | | - // Disable HTML5 validation display |
22 | | - $formElement.attr('novalidate', 'novalidate'); |
| 20 | + // Disable HTML5 validation display |
| 21 | + $formElement.attr('novalidate', 'novalidate'); |
23 | 22 |
|
24 | | - var ngSubmit = $formAttributes.ngSubmit; |
25 | | - /* |
26 | | - * Removing ngSubmit attribute if any since ngSubmit by default doesn't respects the validation errors |
27 | | - * on the input fields. |
28 | | - */ |
29 | | - delete $formAttributes.ngSubmit; |
| 23 | + var ngSubmit = $formAttributes.ngSubmit; |
| 24 | + /* |
| 25 | + * Removing ngSubmit attribute if any since ngSubmit by default doesn't respects the validation errors |
| 26 | + * on the input fields. |
| 27 | + */ |
| 28 | + delete $formAttributes.ngSubmit; |
30 | 29 |
|
31 | | - var preLinkFunction = function($scope, formElement, $attr, formController) { |
32 | | - // Expose a method to manually show the validation state |
33 | | - formController.$showValidation = function() { |
34 | | - formController.$setSubmitted(); |
35 | | - // Tell form elements to show validation state |
36 | | - $scope.$broadcast('onBsValidationStateChange', {showValidationState: true}); |
37 | | - }; |
| 30 | + var preLinkFunction = function ($scope, formElement, $attr, formController) { |
| 31 | + // Expose a method to manually show the validation state |
| 32 | + formController.$showValidation = function () { |
| 33 | + formController.$setSubmitted(); |
| 34 | + // Tell form elements to show validation state |
| 35 | + $scope.$broadcast('onBsValidationStateChange', {showValidationState: true}); |
| 36 | + }; |
38 | 37 |
|
39 | | - formController.$hideValidation = function () { |
40 | | - formController.$setPristine(); |
41 | | - // Tell form elements to hide validation state |
42 | | - $scope.$broadcast('onBsValidationStateChange', {showValidationState: false}); |
43 | | - }; |
| 38 | + formController.$hideValidation = function () { |
| 39 | + formController.$setPristine(); |
| 40 | + // Tell form elements to hide validation state |
| 41 | + $scope.$broadcast('onBsValidationStateChange', {showValidationState: false}); |
| 42 | + }; |
44 | 43 |
|
45 | | - var markPristineAfterSubmit = formElement[0].attributes.hasOwnProperty('bs-pristine-on-submit'); |
| 44 | + var markPristineAfterSubmit = formElement[0].attributes.hasOwnProperty('bs-pristine-on-submit'); |
46 | 45 |
|
47 | | - formElement.on('submit', function(e) { |
48 | | - // If any of the form element has not passed the validation |
49 | | - if (formController.$invalid) { |
50 | | - // Then focus the first invalid element |
51 | | - formElement[0].querySelector('.ng-invalid').focus(); |
52 | | - return false; |
53 | | - } |
| 46 | + formElement.on('submit', function (e) { |
| 47 | + // If any of the form element has not passed the validation |
| 48 | + if (formController.$invalid) { |
| 49 | + // Then focus the first invalid element |
| 50 | + formElement[0].querySelector('.ng-invalid').focus(); |
| 51 | + return false; |
| 52 | + } |
54 | 53 |
|
55 | | - // Parse the handler of ng-submit & execute it |
56 | | - var submitHandler = $parse(ngSubmit); |
57 | | - $scope.$apply(function() { |
58 | | - submitHandler($scope, {$event: e}); |
| 54 | + // Parse the handler of ng-submit & execute it |
| 55 | + var submitHandler = $parse(ngSubmit); |
| 56 | + $scope.$apply(function () { |
| 57 | + submitHandler($scope, {$event: e}); |
59 | 58 |
|
60 | | - formController.$commitViewValue(); |
61 | | - formController.$setSubmitted(); |
62 | | - |
63 | | - if (markPristineAfterSubmit) { |
64 | | - formController.$hideValidation(); |
65 | | - } |
66 | | - }); |
| 59 | + formController.$commitViewValue(); |
| 60 | + formController.$setSubmitted(); |
67 | 61 |
|
68 | 62 | if (markPristineAfterSubmit) { |
69 | | - /** |
70 | | - * Prevent other submit event listener registered via Angular so that we can mark the form with |
71 | | - * the pristine state. Otherwise, that Angular's listener is getting called at the last and is again |
72 | | - * setting form to the submitted. |
73 | | - * |
74 | | - * https://api.jquery.com/event.stopimmediatepropagation/ |
75 | | - */ |
76 | | - e.stopImmediatePropagation(); |
77 | | - e.preventDefault(); |
| 63 | + formController.$hideValidation(); |
78 | 64 | } |
79 | | - |
80 | | - return true; |
81 | 65 | }); |
82 | | - }; |
83 | 66 |
|
84 | | - return { |
85 | | - pre: preLinkFunction |
86 | | - }; |
| 67 | + if (markPristineAfterSubmit) { |
| 68 | + /** |
| 69 | + * Prevent other submit event listener registered via Angular so that we can mark the form with |
| 70 | + * the pristine state. Otherwise, that Angular's listener is getting called at the last and is again |
| 71 | + * setting form to the submitted. |
| 72 | + * |
| 73 | + * https://api.jquery.com/event.stopimmediatepropagation/ |
| 74 | + */ |
| 75 | + e.stopImmediatePropagation(); |
| 76 | + e.preventDefault(); |
| 77 | + } |
| 78 | + |
| 79 | + return true; |
| 80 | + }); |
| 81 | + }; |
| 82 | + |
| 83 | + return { |
| 84 | + pre: preLinkFunction |
| 85 | + }; |
87 | 86 | } |
88 | 87 | }; |
89 | | -}]); |
| 88 | + }]); |
0 commit comments