Skip to content
This repository was archived by the owner on Jul 1, 2020. It is now read-only.

Commit a73505b

Browse files
committed
checkFormValidatity() work with $scope & form obj
- Make the checkFormValidatity() work with both options of $scope alone or a form object $scope.form1 - Added index to readme
1 parent 050f9e5 commit a73505b

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

readme.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ Huge rewrite to have a better code separation and also adding support to Service
2020
## Live Demo
2121
[Plunker](http://plnkr.co/jADq7H)
2222

23+
<a name="index"></a>
24+
## Index
25+
* [Plunker Demo](#plunker)
26+
* [Dependency](#dependencies)
27+
* [Install](#install)
28+
* [Include it in your app project](#project)
29+
* [Requirements](#requirements)
30+
* [Some Working Examples (Directive)](#examples-directive)
31+
* [Some Working Examples (Service)](#examples-service)
32+
* [Form Submit](#submit)
33+
* [Validation summary](#validation-summary)
34+
* [Bootstrap Input Groups Wrapping - HOWTO](#input-groups-wrapping)
35+
* [Regular Expressions (Regex)](#regex)
36+
* [Locales (languages)](#locales)
37+
* [Available Validators](#validators)
38+
* [Changelog](#changelog)
39+
2340
<a name="install"></a>
2441
Install
2542
-----
@@ -65,12 +82,13 @@ myApp.config(function ($translateProvider) {
6582
<script type="text/javascript" src="src/validation-rules.js"></script>
6683
```
6784

85+
<a name="requirements"></a>
6886
## Requirements
6987
Angular-Validation requires the element which will use validation to have an html `name=""` attribute, so that in the background it can use this name to create and show an error into a `<span>` which will directly follow your form input. For example: `<input name="input1" ng-model="input1" validation="required" />`.
7088

7189
*The necessity of `name=""` attribute is new since version 1.3.4+, prior to this change we were asking the user to create his own `<span>` for error displaying. In other words, the `<span>` is now optional, but the `name=""` attribute becomes mandatory and will throw an error if omitted*
7290

73-
<a name="examples-directives"></a>
91+
<a name="examples-directive"></a>
7492
## Some Working Examples (Directive)
7593
Let's start with a simple example and then let's get down to real business.
7694

@@ -175,6 +193,9 @@ The Angular-Validation concept was first introduced with the use `ngDisabled` on
175193
```
176194
```javascript
177195
// Option #2 will need to call the `checkFormValidity()` function checking the form before doing a real submit
196+
// $validationSummary can be found in 2 variables (depending if your form as a name or not)
197+
// you can use `checkFormValidity($scope.form1)` or this `checkFormValidity($scope)`
198+
// If your form does not have a name attribute, your only choice is `checkFormValidity($scope)`
178199
$scope.submitForm = function() {
179200
var myValidation = new validationService();
180201
if(myValidation.checkFormValidity($scope.form1)) {
@@ -211,6 +232,7 @@ Display a validation summary of all the current form errors. Validation summary
211232
</form>
212233
```
213234

235+
<a name="input-groups-wrapping"></a>
214236
Bootstrap Input Groups Wrapping - HOWTO
215237
--------------------
216238
Well let's face it, having the `<span>` for error display right after the element to be validated is not always ideal and I encounter the problem myself when using Bootstrap on inputs with `input-group`, it had so much wrapping around the input that the next available element might not be the one we want. In these special occasions, we will add a `<span>` or a `<div>` for displaying the possible error and give the this element an `id="someId"` or a `class="className"` and then reference it inside our input. We could actually move the error element anywhere we want with this method, just don't forget to name it with an `id` or a `className` and call the `validation-error-to` attribute. This attribute could be called in 3 different ways: with `'.'` (element error className) or with/without `'#'` (element error id) We could even do a validation summary with this...just saying hehe.
@@ -250,7 +272,7 @@ From the example displayed, I introduce the custom regular expression, there is
250272

251273
Regex validation is divided in 4 specific parts (Step #1-4).
252274

253-
Let's use the previous [Examples #5](#examples-directives) and extract the information out of it to see how it works.
275+
Let's use the previous [Examples #5](#examples-directive) and extract the information out of it to see how it works.
254276
Step #1-4 are for explanation only, at the end we show the full regex (make sure there is no spaces).
255277

256278
1. Start &amp; end the filter with the following `regex: :regex` which tells the directive where to extract it.
@@ -263,7 +285,7 @@ Step #1-4 are for explanation only, at the end we show the full regex (make sure
263285

264286
Final code (without spaces): `regex:YYWW:=^(0[9]|1[0-9]|2[0-9]|3[0-9])(5[0-2]|[0-4][0-9])$:regex`
265287

266-
288+
<a name="locales"></a>
267289
Locales (languages)
268290
--------------------
269291
Locales are simple sets of language defined in external JSON files, we can easily add any new language as extra files without affecting the behaviour of the angular directive. You could even change displayed language on the fly (note that the error message will be reflected only after field value is re-evaluated). Make sure to include the `angular-translate` library and configure it, see section [Include it in your Project](#project)
@@ -281,9 +303,9 @@ $scope.switchLanguage = function (key) {
281303
});
282304
};
283305
```
284-
285306
*P.S. If you define a new Language set, please make a pull request and I would be happy to add them in current project... It would be nice to have Spanish, German or even Chinese :) Thank you.*
286307

308+
<a name="validators"></a>
287309
Available Validators
288310
--------------------
289311
All validators are written as `snake_case` but it's up to the user's taste and could also be written as `camelCase`. So for example `alpha_dash_spaces` and `alphaDashSpaces` are both equivalent.
@@ -374,6 +396,7 @@ License
374396
* Add more validators...
375397
* Add more locale languages... I need your help on that one!!!
376398

399+
<a name="changelog"></a>
377400
## CHANGELOG
378401
* [1.3.0](https://github.com/ghiscoding/angular-validation/commit/d106996926bef86a0457c90fbb65fe6233f3928d) `2014-12-01` Added support to AngularJS 1.3
379402
* [1.3.1](https://github.com/ghiscoding/angular-validation/commit/44fe9de050504a46bb0eb975c31bc4b0f3b6f516) `2015-01-02` Added Input Match/Confirmation Validator, ex: password confirmation.

src/validation-service.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,26 @@ angular
9090
} // addValidator()
9191

9292
/** Is the Form all valid? Loop through Validation Summary to get the answer, if any errors are there then display them and return false
93-
* @param object Angular Form Object
93+
* @param object Angular Form or Scope Object
9494
* @return bool isFormValid
9595
*/
96-
function checkFormValidity(form) {
96+
function checkFormValidity(obj) {
9797
var self = this;
9898
var ctrl, elm, elmName = '', isValid = true;
99-
if(typeof form === "undefined") {
100-
throw 'Form validaty checking requires a valid form object passed as argument';
99+
if(typeof obj === "undefined" || typeof obj.$validationSummary === "undefined") {
100+
throw 'Form validaty checking requires a valid Angular Form or $scope object passed as argument to function properly (ex.: $scope.form1 OR $scope).';
101101
}
102102

103103
// loop through $validationSummary and display errors when found on each field
104-
for(var i = 0, ln = form.$validationSummary.length; i < ln; i++) {
104+
for(var i = 0, ln = obj.$validationSummary.length; i < ln; i++) {
105105
isValid = false;
106-
elmName = form.$validationSummary[i].field;
106+
elmName = obj.$validationSummary[i].field;
107107
elm = angular.element(document.querySelector('[name="'+elmName+'"]:not([disabled]):not([ng-disabled]'));
108108
ctrl = angular.element(elm).controller('ngModel');
109109

110110
if(!!elm && elm.length > 0) {
111111
ctrl.$setTouched(); // make the element as it was touched for CSS
112-
self.commonObj.updateErrorMsg(form.$validationSummary[i].message, {valid: false, elm: elm, submitted: true});
112+
self.commonObj.updateErrorMsg(obj.$validationSummary[i].message, {valid: false, elm: elm, submitted: true});
113113
}
114114
}
115115
return isValid;

0 commit comments

Comments
 (0)