Skip to content
This repository was archived by the owner on Jul 1, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes for removing invalid validation re. wizard
Fixes for removing invalid validation re. wizard
  • Loading branch information
tlastad committed Apr 7, 2015
commit b1446eb070793b178bc71df187b9f42db1907181
21 changes: 18 additions & 3 deletions src/validation-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ angular
validationCommon.prototype.removeFromValidationSummary = removeFromValidationSummary; // remove an element from the $validationSummary
validationCommon.prototype.updateErrorMsg = updateErrorMsg; // update on screen an error message below current form element
validationCommon.prototype.validate = validate; // validate current element

validationCommon.prototype.removeFromFormElementObjectList = removeFromFormElementObjectList; // remove named items from formElements list
// return the service object
return validationCommon;

Expand Down Expand Up @@ -180,8 +180,13 @@ angular
* @param object attributes
*/
function updateErrorMsg(message, attrs) {
// attrs.obj if set should be a commonObj
var self = (!!attrs && attrs.obj) ? attrs.obj : this;
var self = this;
// attrs.obj if set, should be a commonObj, and can be self.
// In addition we need to set validatorAttrs, as they are defined as attrs on obj.
if (!!attrs && attrs.obj) {
self = attrs.obj;
self.validatorAttrs = attrs.obj.attrs;
}

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

/** Remove objects from FormElement list.
* @param elementName to remove
*/
function removeFromFormElementObjectList(elmName) {
var index = arrayFindObjectIndex(formElements, 'fieldName', elmName); // find index of object in our array
if (index >= 0) {
formElements.splice(index, 1);
}
}

/** Add the error to the validation summary
* @param object self
* @param string message: error message
Expand Down
27 changes: 26 additions & 1 deletion src/validation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ angular
validationService.prototype.checkFormValidity = checkFormValidity; // check the form validity (can be called by an empty validationService and used by both Directive/Service)
validationService.prototype.removeValidator = removeValidator; // remove a Validator from an element
validationService.prototype.setGlobalOptions = setGlobalOptions; // set and initialize global options used by all validators

validationService.prototype.clearInvalidValidatorsInSummary = clearInvalidValidatorsInSummary; // clear clearInvalidValidatorsInSummary

return validationService;

//----
Expand Down Expand Up @@ -91,6 +92,30 @@ angular
return self;
} // addValidator()

/** Remove all objects in validationsummary and matching objects in FormElementList.
* This is for use in a wizard type setting, where you 'move back' to a previous page in wizard.
* In this case you need to remove invalid validators that will exist in 'the future'.
* @param object Angular Form or Scope Object
*/
function clearInvalidValidatorsInSummary(obj) {
var self = this;
if (typeof obj === "undefined" || typeof obj.$validationSummary === "undefined") {
throw 'checkFormValidity() requires a valid Angular Form or $scope object passed as argument to function properly (ex.: $scope.form1 OR $scope).';
}
// Get list of names to remove
var elmName = [];
for (var i = 0, ln = obj.$validationSummary.length; i < ln; i++) {
elmName.push(obj.$validationSummary[i].field);
}
// Loop on list of names. Cannot loop on obj.$validationSummary as you are removing objects from it in the loop.
for (i = 0, ln = elmName.length; i < ln; i++) {
if (!!elmName[i]) {
self.commonObj.removeFromFormElementObjectList(elmName[i]);
self.commonObj.removeFromValidationSummary(obj.$validationSummary, elmName[i]);
}
}
}

/** Check the form validity (can be called by an empty validationService and used by both Directive/Service)
* Loop through Validation Summary and if any errors found then display them and return false on current function
* @param object Angular Form or Scope Object
Expand Down