Skip to content

Commit 7d92190

Browse files
author
Shashank Agrawal
authored
Merge pull request #5 from julianbei/master
Major refactoring for reusability
2 parents abf8f22 + 6c31e76 commit 7d92190

File tree

5 files changed

+291
-286
lines changed

5 files changed

+291
-286
lines changed

bower.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{
22
"name": "bootstrap-angular-validation",
33
"description": "Bootstrap form validation in Angular like jQuery but w/o jQuery.",
4-
"main": "src/app.js",
5-
"dependencies": {
6-
"bootstrap": "^3.3.6"
7-
},
4+
"main": "dist/bootstrap-angular-validation.min.js",
5+
"dependencies": { },
86
"devDependencies": {
97
"angular": "^1.5.5"
108
},
119
"authors": [
12-
"Shashank Agrawal <shagrawal99@gmail.com>"
10+
"Shashank Agrawal <shagrawal99@gmail.com>",
11+
"Julian Amelung <j.beisenkoetter@gmail.com>"
1312
],
1413
"license": "MIT",
1514
"ignore": [

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: 78 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,101 @@
1-
"use strict";
1+
'use strict';
22

33
/**
44
* @ngdoc directive
55
* @name form
6-
* @requires $parse
6+
* @requires $parse
77
* @requires $rootScope
8-
*
8+
*
99
* @description
10-
* Using form element as directive, we don't require to put the "bs-validation" directive to every form element.
10+
* Using form element as directive, we don't require to put the 'bs-validation' directive to every form element.
1111
* To add handler on submit, use <code>on-submit</code> instead of <code>ng-submit</code>,
1212
* since ng-submit directive doesn't cares about validation errors.
1313
*/
14-
angular.module("bootstrap.angular.validation").directive("form", ["$parse", "$rootScope", "BsValidationService",
15-
function($parse, $rootScope, bsValidationService) {
16-
14+
angular.module('bootstrap.angular.validation').directive('form', [
15+
'$parse',
16+
'$rootScope',
17+
'BsValidationService',
18+
function($parse, $rootScope, bsValidationService) {
1719
return {
18-
restrict: "E",
19-
require: "form",
20-
priority: 1000, // Setting a higher priority so that, this directive compiles first.
21-
compile: function($formElement, $formAttributes) {
22-
// Disable HTML5 validation display
23-
$formElement.attr("novalidate", "novalidate");
24-
bsValidationService.addDirective($formElement);
20+
restrict: 'E',
21+
require: 'form',
22+
priority: 1000, // Setting a higher priority so that, this directive compiles first.
23+
compile: function($formElement, $formAttributes) {
24+
// Disable HTML5 validation display
25+
$formElement.attr('novalidate', 'novalidate');
26+
bsValidationService.addDirective($formElement);
27+
28+
/**
29+
* If there is an 'ng-include' directive available inside a form then the 'bs-validation' directive
30+
* won't get applied until Angular resolves the view sourced by 'ng-include'.
31+
*/
32+
var nestedNgIncludeElement = $formElement.findAll('[ng-include]');
33+
if (nestedNgIncludeElement.length > 0) {
34+
var src = $parse(nestedNgIncludeElement.attr('ng-include'))();
2535

2636
/**
27-
* If there is an "ng-include" directive available inside a form then the "bs-validation" directive
28-
* won't get applied until Angular resolves the view sourced by "ng-include".
37+
* Then add the source URL of ng-include to a list so that in the response interceptor, we can add
38+
* the 'bs-validation' directive. We can do this by recompiling here the content after the content is
39+
* loaded but that leads to some problem and also increases the rendering time.
40+
*
41+
* @see response interceptor in app.js
2942
*/
30-
var nestedNgIncludeElement = $formElement.findAll("[ng-include]");
31-
if (nestedNgIncludeElement.length > 0) {
32-
var src = $parse(nestedNgIncludeElement.attr("ng-include"))();
43+
bsValidationService.addToNgIncludedURLs(src);
44+
}
3345

34-
/**
35-
* Then add the source URL of ng-include to a list so that in the response interceptor, we can add
36-
* the "bs-validation" directive. We can do this by recompiling here the content after the content is
37-
* loaded but that leads to some problem and also increases the rendering time.
38-
*
39-
* @see response interceptor in app.js
40-
*/
41-
bsValidationService.addToNgIncludedURLs(src);
42-
}
43-
44-
var ngSubmit = $formAttributes.ngSubmit;
45-
/*
46-
* Removing ngSubmit attribute if any since ngSubmit by default doesn't respects the validation errors
47-
* on the input fields.
48-
*/
49-
delete $formAttributes.ngSubmit;
46+
var ngSubmit = $formAttributes.ngSubmit;
47+
/*
48+
* Removing ngSubmit attribute if any since ngSubmit by default doesn't respects the validation errors
49+
* on the input fields.
50+
*/
51+
delete $formAttributes.ngSubmit;
5052

51-
var preLinkFunction = function($scope, formElement, $attr, formController) {
52-
// Expose a method to manually trigger the validation
53-
formController.$validate = function() {
54-
formController.$setSubmitted();
55-
};
53+
var preLinkFunction = function($scope, formElement, $attr, formController) {
54+
// Expose a method to manually trigger the validation
55+
formController.$validate = function() {
56+
formController.$setSubmitted();
57+
};
5658

57-
formElement.on("submit", function(e) {
58-
e.preventDefault();
59+
formElement.on('submit', function(e) {
60+
e.preventDefault();
5961

60-
// If any of the form element has not passed the validation
61-
if (formController.$invalid) {
62-
// Then focus the first invalid element
63-
formElement[0].querySelector(".ng-invalid").focus();
64-
return false;
65-
}
62+
// If any of the form element has not passed the validation
63+
if (formController.$invalid) {
64+
// Then focus the first invalid element
65+
formElement[0].querySelector('.ng-invalid').focus();
66+
return false;
67+
}
6668

67-
// Parse the handler of ng-submit & execute it
68-
var submitHandler = $parse(ngSubmit);
69-
$scope.$apply(function() {
70-
submitHandler($scope, {$event: e});
69+
// Parse the handler of ng-submit & execute it
70+
var submitHandler = $parse(ngSubmit);
71+
$scope.$apply(function() {
72+
submitHandler($scope, {$event: e});
7173

72-
formController.$commitViewValue();
73-
formController.$setSubmitted();
74+
formController.$commitViewValue();
75+
formController.$setSubmitted();
7476

75-
/*
76-
* Do not show validation errors once the form gets submitted. You can still display the
77-
* validation errors after form submission by calling "$setSubmitted" in your form controller.
78-
*/
79-
formController.$setPristine();
80-
});
77+
/*
78+
* Do not show validation errors once the form gets submitted. You can still display the
79+
* validation errors after form submission by calling '$setSubmitted' in your form controller.
80+
*/
81+
formController.$setPristine();
82+
});
8183

82-
/**
83-
* Prevent other submit event listener registered via Angular so that we can mark the form with
84-
* the prestine state. Otherwise, that Angular's listener is getting called at the last and is again
85-
* setting form to the submitted.
86-
*
87-
* https://api.jquery.com/event.stopimmediatepropagation/
88-
*/
89-
e.stopImmediatePropagation();
90-
return false;
91-
});
92-
};
84+
/**
85+
* Prevent other submit event listener registered via Angular so that we can mark the form with
86+
* the prestine state. Otherwise, that Angular's listener is getting called at the last and is again
87+
* setting form to the submitted.
88+
*
89+
* https://api.jquery.com/event.stopimmediatepropagation/
90+
*/
91+
e.stopImmediatePropagation();
92+
return false;
93+
});
94+
};
9395

94-
return {
95-
pre: preLinkFunction
96-
};
97-
}
96+
return {
97+
pre: preLinkFunction
98+
};
99+
}
98100
};
99-
}]);
101+
}]);

0 commit comments

Comments
 (0)