1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-12-12 14:56:58 +01:00
bitwarden-browser/src/app/services/validationService.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-12-09 04:35:05 +01:00
angular
.module('bit.services')
.factory('validationService', function () {
var _service = {};
_service.addErrors = function (form, reason) {
var data = reason.data;
2017-05-09 20:23:39 +02:00
var defaultErrorMessage = 'An unexpected error has occurred.';
2015-12-09 04:35:05 +01:00
form.$errors = [];
if (!data || !angular.isObject(data)) {
form.$errors.push(defaultErrorMessage);
return;
}
if (data && data.ErrorModel) {
data = data.ErrorModel;
}
2015-12-09 04:35:05 +01:00
if (!data.ValidationErrors) {
if (data.Message) {
form.$errors.push(data.Message);
}
else {
form.$errors.push(defaultErrorMessage);
}
return;
}
for (var key in data.ValidationErrors) {
if (!data.ValidationErrors.hasOwnProperty(key)) {
continue;
}
for (var i = 0; i < data.ValidationErrors[key].length; i++) {
_service.addError(form, key, data.ValidationErrors[key][i]);
}
}
};
_service.addError = function (form, key, errorMessage, clearExistingErrors) {
if (clearExistingErrors || !form.$errors) {
form.$errors = [];
}
var pushError = true;
for (var i = 0; i < form.$errors.length; i++) {
2016-09-18 05:00:05 +02:00
if (form.$errors[i] === errorMessage) {
pushError = false;
break;
}
}
if (pushError) {
form.$errors.push(errorMessage);
}
2015-12-09 04:35:05 +01:00
if (key && key !== '' && form[key] && form[key].$registerApiError) {
form[key].$registerApiError();
}
};
return _service;
});