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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-28 07:19:43 +01:00
|
|
|
|
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 = [];
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-27 05:15:35 +01:00
|
|
|
|
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) {
|
2015-12-27 05:15:35 +01:00
|
|
|
|
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;
|
|
|
|
|
});
|