This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
site-base/spark/resources/assets/js/forms/errors.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-11-03 05:26:07 +00:00
/**
* Spark form error collection class.
*/
window.SparkFormErrors = function () {
this.errors = {};
/**
* Determine if the collection has any errors.
*/
this.hasErrors = function () {
return ! _.isEmpty(this.errors);
};
/**
* Determine if the collection has errors for a given field.
*/
this.has = function (field) {
return _.indexOf(_.keys(this.errors), field) > -1;
};
/**
* Get all of the raw errors for the collection.
*/
this.all = function () {
return this.errors;
};
/**
* Get all of the errors for the collection in a flat array.
*/
this.flatten = function () {
return _.flatten(_.toArray(this.errors));
};
/**
* Get the first error message for a given field.
*/
this.get = function (field) {
if (this.has(field)) {
return this.errors[field][0];
}
};
/**
* Set the raw errors for the collection.
*/
this.set = function (errors) {
if (typeof errors === 'object') {
this.errors = errors;
} else {
this.errors = {'form': ['Something went wrong. Please try again or contact customer support.']};
}
};
/**
* Remove errors from the collection.
*/
this.forget = function (field) {
if (typeof field === 'undefined') {
this.errors = {};
} else {
Vue.delete(this.errors, field);
}
};
};