pip-webui
Version:
HTML5 UI for LOB applications
176 lines (153 loc) • 6.13 kB
JavaScript
/**
* @file Form error utilities
* @copyright Digital Living Software Corp. 2014-2016
*
*/
/* global _, angular */
(function () {
'use strict';
var thisModule = angular.module('pipUtils.FormErrors', []);
thisModule.factory('pipFormErrors', function ($rootScope) {
return {
errorsWithHint: errorsWithHint,
//submittedErrors: submittedErrors,
//submittedErrorsWithHint: submittedErrorsWithHint,
//dirtyErrors: dirtyErrors,
//dirtyErrorsWithHint: dirtyErrorsWithHint,
//touchedErrors: touchedErrors,
touchedErrorsWithHint: touchedErrorsWithHint,
resetFormErrors: resetFormErrors,
setFormError: setFormError,
resetFieldsErrors: resetFieldsErrors
};
//-------------------
function errorsWithHint(field) {
if (field == null) return;
return _.isEmpty(field.$error) ? { hint: true } : field.$error;
};
// function submittedErrors(form, field) {
// if (form == null) throw new Error('Form is not set');
// if (field == null) throw new Error('Field is not set');
//
// if (form.$submitted)
// return field.$error;
// return {};
// };
//
// function submittedErrorsWithHint(form, field) {
// if (form == null) throw new Error('Form is not set');
// if (field == null) throw new Error('Field is not set');
//
// if (form.$submitted) {
// return _.isEmpty(field.$error) ? { hint: true} : field.$error;
// }
// return { hint: true };
// };
//
// function dirtyErrors(form, field) {
// if (form == null) throw new Error('Form is not set');
// if (field == null) throw new Error('Field is not set');
//
// if (field.$dirty || form.$dirty)
// return field.$error;
// return {};
// };
//
// function dirtyErrorsWithHint(form, field) {
// if (form == null) throw new Error('Form is not set');
// if (field == null) throw new Error('Field is not set');
//
// if (field.$dirty || form.$dirty) {
// return _.isEmpty(field.$error) ? { hint: true} : field.$error;
// }
// return { hint: true };
// };
//
// function touchedErrors(form, field) {
// if (form == null) throw new Error('Form is not set');
// if (field == null) throw new Error('Field is not set');
//
// if (field.$touched || form.$dirty)
// return field.$error;
// return {};
// };
function touchedErrorsWithHint(form, field) {
if (form == null) return;
if (field == null) return;
if (form.$submitted && (field.$touched || form.$dirty) || !form.$submitted && (field.$touched || field.$dirty)) {
var result = _.isEmpty(field.$error) ? { hint: true} : field.$error;
return result;
}
return { hint: true };
};
function resetFormErrors(form, errors) {
form.$setPristine();
form.$setUntouched();
if (errors) {
form.$setDirty();
form.$setSubmitted();
}
form.$serverError = {};
};
function resetFieldsErrors(form, field) {
if (!form) return;
if (field && form[field] && form[field].$error) {
form[field].$error = {};
} else {
for (var prop in form) {
if (form[prop] && form[prop].$error) {
form[prop].$error = {};
};
}
if (form && form.$error) form.$error = {};
}
};
function setFormError(form, error, errorFieldMap) {
if (error == null) return;
// Prepare form server errors
form.$serverError = form.$serverError || {};
// Prepare error code
var code = error.code || (error.data || {}).code || null;
if (!code && error.status) code = error.status;
if (code) {
var
errorName = 'ERROR_' + code,
field = errorFieldMap ? errorFieldMap[code] : null;
// Set server error to fields
if (field && form[field] && form[field].$setValidity) {
form[field].$setValidity(errorName, false);
return;
}
// Set server error to form
if (field == 'form') {
form.$serverError[errorName] = true;
return;
}
}
// if undefined error for this form or code === undefined/null, go to unhandled error page
if (error.data && error.data.message) {
form.$serverError['ERROR_UNKNOWN'] = error.data.message;
goToUnhandledErrorPage(error);
return;
}
// Set as undefined error
if (error.message) {
form.$serverError['ERROR_UNKNOWN'] = error.message;
goToUnhandledErrorPage(error);
return;
}
if (error.name) {
form.$serverError['ERROR_UNKNOWN'] = error.name;
goToUnhandledErrorPage(error);
return;
}
form.$serverError['ERROR_UNKNOWN'] = error;
goToUnhandledErrorPage(error);
};
function goToUnhandledErrorPage(error) {
$rootScope.$emit('pipUnhandledInternalError', {
error: error
});
};
});
})();