showErrors.js 1.34 KB
export default function (errors, errorDiv, mainDiv) {
    mainDiv = mainDiv || document;

    // check if we have any actual errors
    if (errors === undefined || errors === null) {
        return;
    }

    // Turn OResult into array of errors.
    if (errors.errorMsg) {
        errors = [errors.errorMsg];
    }

    // Turn OResults into array of errors.
    if (errors.OResults) {
        errors = errors.OResults;
    }

    // Prepend the main div with the new error div if one does not exist.
    if ($(errorDiv, mainDiv).length < 1) {
        $(mainDiv).prepend('<div id=' + errorDiv + '></div>');
    }

    // Reset the error div.
    $(errorDiv, mainDiv).html('<ul></ul>');
    $(errorDiv, mainDiv).addClass('error');

    for (var key in errors) {
        if (!errors.hasOwnProperty(key)) {
            continue;
        }

        let errorMsg = '';

        switch (key) {
            case 'fullName':
                errorMsg = 'Full Name ' + errors[key].errorMsg;
                break;
            default:
                if (typeof errors[key] === 'string') {
                    errorMsg = errors[key];
                } else {
                    errorMsg = errors[key].errorMsg;
                }
                break;
        }

        $(errorDiv + ' ul', mainDiv).append('<li>' + errorMsg + '</li>');
        $(errorDiv, mainDiv).show();
    }
}