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

Commit b1446eb

Browse files
committed
Fixes for removing invalid validation re. wizard
Fixes for removing invalid validation re. wizard
1 parent 580d199 commit b1446eb

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/validation-common.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ angular
5656
validationCommon.prototype.removeFromValidationSummary = removeFromValidationSummary; // remove an element from the $validationSummary
5757
validationCommon.prototype.updateErrorMsg = updateErrorMsg; // update on screen an error message below current form element
5858
validationCommon.prototype.validate = validate; // validate current element
59-
59+
validationCommon.prototype.removeFromFormElementObjectList = removeFromFormElementObjectList; // remove named items from formElements list
6060
// return the service object
6161
return validationCommon;
6262

@@ -180,8 +180,13 @@ angular
180180
* @param object attributes
181181
*/
182182
function updateErrorMsg(message, attrs) {
183-
// attrs.obj if set should be a commonObj
184-
var self = (!!attrs && attrs.obj) ? attrs.obj : this;
183+
var self = this;
184+
// attrs.obj if set, should be a commonObj, and can be self.
185+
// In addition we need to set validatorAttrs, as they are defined as attrs on obj.
186+
if (!!attrs && attrs.obj) {
187+
self = attrs.obj;
188+
self.validatorAttrs = attrs.obj.attrs;
189+
}
185190

186191
// element name could be defined in the `attrs` or in the self object
187192
var elm = (!!attrs && attrs.elm) ? attrs.elm : self.elm;
@@ -371,6 +376,16 @@ angular
371376
return formElements;
372377
}
373378

379+
/** Remove objects from FormElement list.
380+
* @param elementName to remove
381+
*/
382+
function removeFromFormElementObjectList(elmName) {
383+
var index = arrayFindObjectIndex(formElements, 'fieldName', elmName); // find index of object in our array
384+
if (index >= 0) {
385+
formElements.splice(index, 1);
386+
}
387+
}
388+
374389
/** Add the error to the validation summary
375390
* @param object self
376391
* @param string message: error message

src/validation-service.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +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-
31+
validationService.prototype.clearInvalidValidatorsInSummary = clearInvalidValidatorsInSummary; // clear clearInvalidValidatorsInSummary
32+
3233
return validationService;
3334

3435
//----
@@ -91,6 +92,30 @@ angular
9192
return self;
9293
} // addValidator()
9394

95+
/** 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.
97+
* In this case you need to remove invalid validators that will exist in 'the future'.
98+
* @param object Angular Form or Scope Object
99+
*/
100+
function clearInvalidValidatorsInSummary(obj) {
101+
var self = this;
102+
if (typeof obj === "undefined" || typeof obj.$validationSummary === "undefined") {
103+
throw 'checkFormValidity() requires a valid Angular Form or $scope object passed as argument to function properly (ex.: $scope.form1 OR $scope).';
104+
}
105+
// Get list of names to remove
106+
var elmName = [];
107+
for (var i = 0, ln = obj.$validationSummary.length; i < ln; i++) {
108+
elmName.push(obj.$validationSummary[i].field);
109+
}
110+
// Loop on list of names. Cannot loop on obj.$validationSummary as you are removing objects from it in the loop.
111+
for (i = 0, ln = elmName.length; i < ln; i++) {
112+
if (!!elmName[i]) {
113+
self.commonObj.removeFromFormElementObjectList(elmName[i]);
114+
self.commonObj.removeFromValidationSummary(obj.$validationSummary, elmName[i]);
115+
}
116+
}
117+
}
118+
94119
/** Check the form validity (can be called by an empty validationService and used by both Directive/Service)
95120
* Loop through Validation Summary and if any errors found then display them and return false on current function
96121
* @param object Angular Form or Scope Object

0 commit comments

Comments
 (0)