-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathform-errors.js
70 lines (61 loc) · 1.34 KB
/
form-errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { flatten, has, isEmpty, toArray } from 'lodash'
class FormErrors
{
/*
* Create a new FormErrors instance.
*/
constructor() {
this.errors = {};
}
/*
* Get all of the raw errors for the collection.
*/
all() {
return this.errors;
}
/*
* Determine if the collection has any errors.
*/
hasErrors() {
return ! isEmpty(this.errors);
}
/*
* Get all of the errors for the collection in a flat array.
*/
flatten() {
return flatten(toArray(this.errors));
}
/*
* Forget all of the errors currently in the collection.
*/
forget() {
this.errors = {};
}
/*
* Get the first error for the given field.
*/
get(field) {
if (this.has(field)) {
return this.errors[field][0];
}
}
/*
* Determine if the collection has any errors for the given field.
*/
has(field) {
return has(this.errors, field);
}
/*
* Set the raw errors for the collection.
*/
set(errors) {
if (typeof errors === 'object') {
this.errors = errors;
} else {
this.errors = {
form: ['Something went wrong. Please try again or contact customer support.']
}
}
}
}
export default FormErrors;