1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-05 18:47:52 +01:00
bitwarden-browser/src/Vault/wwwroot/app/services/validationService.js

53 lines
1.5 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;
var defaultErrorMessage = 'An unexpected error has occured.';
form.$errors = [];
if (!data || !angular.isObject(data)) {
form.$errors.push(defaultErrorMessage);
return;
}
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 = [];
}
form.$errors.push(errorMessage);
if (key && key !== '' && form[key] && form[key].$registerApiError) {
form[key].$registerApiError();
}
};
return _service;
});