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

Commit 24037e4

Browse files
committed
Fixed #23 - $validationSummary problems 2+ forms
Fixed #23 - If multiple forms exist in the app the errors in 1 form affect validation in the other
1 parent 1c67eb8 commit 24037e4

13 files changed

+144
-21
lines changed

app.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ myApp.config(['$compileProvider', '$locationProvider', '$routeProvider', functio
99
templateUrl: 'templates/testingFormDirective.html',
1010
controller: 'CtrlValidationDirective'
1111
})
12+
.when('/validate-2forms', {
13+
templateUrl: 'templates/testing2Forms.html',
14+
controller: 'Ctrl2forms'
15+
})
1216
.when('/validate-service', {
1317
templateUrl: 'templates/testingFormService.html',
1418
controller: 'CtrlValidationService'
@@ -54,6 +58,17 @@ myApp.controller('CtrlValidationDirective', ['$scope', 'validationService', func
5458
}
5559
}]);
5660

61+
myApp.controller('Ctrl2forms', ['$scope', 'validationService', function ($scope, validationService) {
62+
$scope.submitForm = function() {
63+
if(new validationService().checkFormValidity($scope.form01)) {
64+
alert('All good, proceed with submit...');
65+
}
66+
}
67+
$scope.showValidationSummary = function () {
68+
$scope.displayValidationSummary = true;
69+
}
70+
}]);
71+
5772
// -- Controller to use Angular-Validation Service
5873
// -----------------------------------------------
5974

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ghiscoding.angular-validation",
3-
"version": "1.3.14",
3+
"version": "1.3.15",
44
"authors": [
55
"Ghislain B."
66
],

changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ Angular-Validation change logs
1515
1.3.11 (2015-03-30): Accepted pull request #15 to fix form without name attribute. Also accepted pull request #18 to add Spanish locales.
1616
1.3.12 (2015-04-04): Fix issue #16 and added Validators Alternate Text option on all type of validators. Also fixed removeValidator and clean a lot of code.
1717
1.3.13 (2015-04-06) Fixed $translate delay issue when using external JSON files
18-
1.3.14 (2015-04-07) Merge pull request #19 Added norwegian translation and changes to allow user to remove invalid validators.
18+
1.3.14 (2015-04-07) Merge pull request #19 Added norwegian translation and changes to allow user to remove invalid validators.
19+
1.3.15 (2015-04-08) Fixed #23 If multiple forms exist in the app the errors in 1 form affect validation in the other

dist/angular-validation.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ <h1>Angular-Validation Directive|Service (ghiscoding)</h1>
2727
<div class="btn-group btn-group-sm">
2828
<button type="button" class="btn btn-default" ng-click="goto('/validate-directive')">Directive</button>
2929
<button type="button" class="btn btn-default" ng-click="goto('/validate-service')">Service</button>
30+
<button type="button" class="btn btn-default" ng-click="goto('/validate-2forms')">2 Forms</button>
3031
</div>
3132

3233
<br/><hr/>

locales/validation/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"INVALID_REQUIRED": "Field is required. ",
5252
"INVALID_URL": "Must be a valid URL. ",
5353
"INVALID_TIME": "Must be a valid time format (hh:mm) OR (hh:mm:ss). ",
54-
"INVALID_CHECKBOX_SELECTED": "Checkbox must be selected",
54+
"INVALID_CHECKBOX_SELECTED": "Checkbox must be selected. ",
5555

5656
"AREA1": "TextArea: Alphanumeric + Minimum(15) + Required",
5757
"ERRORS": "Errors",

locales/validation/es.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"INVALID_REQUIRED": "El campo es requerido. ",
5252
"INVALID_URL": "Debe contener una dirección URL valida. ",
5353
"INVALID_TIME": "Debe contener un formato de tiempo valido (hh:mm) ó (hh:mm:ss). ",
54-
"INVALID_CHECKBOX_SELECTED": "Checkbox must be selected",
54+
"INVALID_CHECKBOX_SELECTED": "Casilla de verificación debe ser seleccionado. ",
5555

5656
"AREA1": "Area de texto: Alfanúmerica + Minimo(15) + Requerido",
5757
"ERRORS": "Errores",

locales/validation/fr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"INVALID_REQUIRED": "Le champ est requis. ",
5252
"INVALID_URL": "Doit être un URL valide. ",
5353
"INVALID_TIME": "Doit être un format de date valide (hh:mm) OU (hh:mm:ss). ",
54-
"INVALID_CHECKBOX_SELECTED": "Checkbox must be selected",
54+
"INVALID_CHECKBOX_SELECTED": "La case à cocher doit être sélectionnée. ",
5555

5656
"AREA1": "TextArea: Alphanumérique + Minimum(15) + Required",
5757
"ERRORS": "Erreurs",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ghiscoding.angular-validation",
3-
"version": "1.3.14",
3+
"version": "1.3.15",
44
"author": "Ghislain B.",
55
"description": "Angular-Validation Directive and Service (ghiscoding)",
66
"main": "app.js",

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Angular Validation (Directive / Service)
2-
`Version: 1.3.14`
2+
`Version: 1.3.15`
33
### Form validation after user inactivity of default 1sec. (customizable timeout)
44

55
Forms Validation with Angular made easy! Angular-Validation is an angular directive/service with locales (languages) with a very simple approach of defining your `validation=""` directly within your element to validate (input, textarea, etc) and...that's it!!! The directive/service will take care of the rest!
@@ -459,4 +459,5 @@ License
459459
* [1.3.11](https://github.com/ghiscoding/angular-validation/commit/e807584f0bcdf0f28ef2ef905b6bc4e890926ac1) `2015-03-30` Accepted pull request #15 to fix form without name attribute. Also accepted pull request #18 to add Spanish locales.
460460
* [1.3.12](https://github.com/ghiscoding/angular-validation/commit/0af82337a6961923e3b022a19660237d3e6f7184) `2015-04-04` Fix issue #16 and added Validators Alternate Text option on all type of validators. Also fixed removeValidator and clean a lot of code.
461461
* [1.3.13](https://github.com/ghiscoding/angular-validation/commit/d0440bdd7fc2816e03d28ad3a9c3bd7bee8ac519) `2015-04-06` Fixed $translate delay issue when using external JSON files
462-
* [1.3.14](https://github.com/ghiscoding/angular-validation/pull/19) `2015-04-07` Merge pull request #19 Added norwegian translation and changes to allow user to remove invalid validators
462+
* [1.3.14](https://github.com/ghiscoding/angular-validation/pull/19) `2015-04-07` Merge pull request #19 Added norwegian translation and changes to allow user to remove invalid validators
463+
* [1.3.15]() `2015-04-08` Fixed #23 If multiple forms exist in the app the errors in 1 form affect validation in the other

src/validation-common.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ angular
2626
var validators = []; // Array of all Form Validators
2727
var validatorAttrs = {}; // Current Validator attributes
2828
var validationSummary = []; // Array Validation Error Summary
29+
var validationSummaries = {}; // Validation Error Summaries separated by Form (could be multiple forms)
2930
var $translate = $filter('translate');
3031

3132
// service constructor
@@ -396,13 +397,14 @@ angular
396397
*/
397398
function addToValidationSummary(self, message) {
398399
var elmName = self.elm.attr('name');
399-
var index = arrayFindObjectIndex(validationSummary, 'field', elmName); // find index of object in our array
400+
var form = getElementParentForm(self); // find the parent form (only found if it has a name)
401+
var index = arrayFindObjectIndex(validationSummary, 'field', elmName); // find index of object in our array
400402

401403
// if message is empty, remove it from the validation summary
402404
if(index >= 0 && message === '') {
403405
validationSummary.splice(index, 1);
404406
}else if(message !== '') {
405-
var errorObj = { field: elmName, message: message };
407+
var errorObj = { field: elmName, message: message, formName: (!!form) ? form.$name : null };
406408

407409
// if error already exist then refresh the error object inside the array, else push it to the array
408410
if(index >= 0) {
@@ -412,11 +414,14 @@ angular
412414
}
413415
}
414416

415-
// save validation summary 2 variable locations, inside the scope object and also in the form object (if found)
417+
// save validation summary scope root
416418
self.scope.$validationSummary = validationSummary;
417-
var form = getScopeForm(self);
419+
420+
// and also save it inside the current scope form (if found)
418421
if (!!form) {
419-
form.$validationSummary = validationSummary;
422+
// since validationSummary contain errors of all forms
423+
// we need to find only the errors of current form and them into the current scope form object
424+
form.$validationSummary = arrayFindObjects(validationSummary, 'formName', form.$name);
420425
}
421426
}
422427

@@ -435,6 +440,22 @@ angular
435440
return null;
436441
}
437442

443+
/** Quick function to find all object(s) inside an array of objects by it's given field name and value, return array of object found(s) or empty array
444+
* @param Array sourceArray
445+
* @param string searchId: search property id
446+
* @param string searchValue: value to search
447+
* @return array of object found from source array
448+
*/
449+
function arrayFindObjects(sourceArray, searchId, searchValue) {
450+
var results = [];
451+
for (var i = 0; i < sourceArray.length; i++) {
452+
if (sourceArray[i][searchId] === searchValue) {
453+
results.push(sourceArray[i]);
454+
}
455+
}
456+
return results;
457+
}
458+
438459
/** Quick function to find an object inside an array by it's given field name and value, return the index position found or -1
439460
* @param Array sourceArray
440461
* @param string searchId: search property id
@@ -450,6 +471,24 @@ angular
450471
return -1;
451472
}
452473

474+
/** Get the element's parent Angular form (if found)
475+
* @param object self
476+
* @return object scope form
477+
*/
478+
function getElementParentForm(self) {
479+
// from the element passed, get his parent form
480+
var elmName = self.elm.attr('name');
481+
var forms = document.getElementsByName(elmName);
482+
483+
for (var i = 0; i < forms.length; i++) {
484+
var form = forms[i].form;
485+
if (!!form && form.name && self.scope[form.name]) {
486+
return self.scope[form.name];
487+
}
488+
}
489+
return null;
490+
}
491+
453492
/** Get form within scope (if found)
454493
* @param object self
455494
* @return object scope form

src/validation-service.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ angular
2828
validationService.prototype.checkFormValidity = checkFormValidity; // check the form validity (can be called by an empty validationService and used by both Directive/Service)
2929
validationService.prototype.removeValidator = removeValidator; // remove a Validator from an element
3030
validationService.prototype.setGlobalOptions = setGlobalOptions; // set and initialize global options used by all validators
31-
validationService.prototype.clearInvalidValidatorsInSummary = clearInvalidValidatorsInSummary; // clear clearInvalidValidatorsInSummary
32-
31+
validationService.prototype.clearInvalidValidatorsInSummary = clearInvalidValidatorsInSummary; // clear clearInvalidValidatorsInSummary
32+
3333
return validationService;
3434

3535
//----
@@ -93,9 +93,9 @@ angular
9393
} // addValidator()
9494

9595
/** Remove all objects in validationsummary and matching objects in FormElementList.
96-
* This is for use in a wizard type setting, where you 'move back' to a previous page in wizard.
96+
* This is for use in a wizard type setting, where you 'move back' to a previous page in wizard.
9797
* In this case you need to remove invalid validators that will exist in 'the future'.
98-
* @param object Angular Form or Scope Object
98+
* @param object Angular Form or Scope Object
9999
*/
100100
function clearInvalidValidatorsInSummary(obj) {
101101
var self = this;

templates/testing2Forms.html

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<h3>Directive - 2 Forms</h3>
2+
3+
<div class="checkbox text-right" style="margin-top:-25px">
4+
<label>
5+
<input type="checkbox" ng-model="displayValidationSummary"> {{ 'SHOW_VALIDATION_SUMMARY' | translate }}
6+
</label>
7+
</div>
8+
9+
<hr />
10+
11+
<!-- Angular-Validation saves the summary of errors inside 2 locations: 1- $scope.$validationSummary 2- $scope.formName.$validationSummary (only accessible if you named your form) so it becomes easy to show all errors -->
12+
<div class="alert alert-danger alert-dismissable" ng-show="displayValidationSummary">
13+
<button type="button" class="close" data-dismiss="alert" aria-hidden="true" ng-click="displayValidationSummary = false">&times;</button>
14+
<h4><strong>{{ 'ERRORS' | translate }}!</strong></h4>
15+
<ul>
16+
<li ng-repeat="item in form01.$validationSummary">{{item.field }}: {{item.message}}</li>
17+
</ul>
18+
</div>
19+
20+
<form novalidate name="form01" method="POST">
21+
<fieldset>
22+
<div class="form-group">
23+
<label for="input2">{{ 'INPUT2' | translate }}</label>
24+
<input type="number" class="form-control" name="input2" placeholder="numeric_signed|required" ng-model="input2" validation="numeric_signed|required" />
25+
</div>
26+
<div class="form-group">
27+
<label for="input3">{{ 'INPUT3' | translate }}</label>
28+
<input type="number" class="form-control" name="input3" placeholder="float_signed|between_num:-0.6,99.5|required" ng-model="input3" validation="float_signed|between_num:-0.6,99.5|required" />
29+
</div>
30+
</fieldset>
31+
32+
<div class="form-actions">
33+
<button type="submit" name="save_btn" class="btn btn-primary" ng-disabled="form01.$invalid" >{{ 'SAVE' | translate }}</button>
34+
</div>
35+
</form>
36+
37+
<hr />
38+
39+
<div class="alert alert-danger alert-dismissable" ng-show="displayValidationSummary">
40+
<button type="button" class="close" data-dismiss="alert" aria-hidden="true" ng-click="displayValidationSummary = false">&times;</button>
41+
<h4><strong>{{ 'ERRORS' | translate }}!</strong></h4>
42+
<ul>
43+
<li ng-repeat="item in form02.$validationSummary">{{item.field }}: {{item.message}}</li>
44+
</ul>
45+
</div>
46+
<form novalidate name="form02" method="POST">
47+
<fieldset>
48+
<div class="form-group">
49+
<label for="select1">{{ 'SELECT1' | translate }}</label>
50+
<select class="form-control" name="select1" ng-model="select1" validation="required:alt={{ 'CHANGE_LANGUAGE' | translate }}">
51+
<option value="">...</option>
52+
<option value="en">English</option>
53+
<option value="es">Español</option>
54+
<option value="fr">French</option>
55+
</select>
56+
</div>
57+
<div class="form-group">
58+
<label for="area1">{{ 'AREA1' | translate }}</label>
59+
<textarea class="form-control" name="area1" rows="3" placeholder="alpha_dash_spaces|min_len:15|required" validation="alpha_dash_spaces|min_len:15|required" ng-model="area1"></textarea>
60+
</div>
61+
</fieldset>
62+
63+
<div class="form-actions">
64+
<button type="submit" name="save_btn" class="btn btn-primary" ng-disabled="form02.$invalid" >{{ 'SAVE' | translate }}</button>
65+
</div>
66+
</form>

0 commit comments

Comments
 (0)