hof 19.13.8 → 19.13.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/date/index.js +34 -23
- package/package.json +1 -1
package/components/date/index.js
CHANGED
@@ -60,6 +60,37 @@ module.exports = (key, opts) => {
|
|
60
60
|
dayOptional = true;
|
61
61
|
}
|
62
62
|
|
63
|
+
// take the 3 date parts, padding or defaulting
|
64
|
+
// to '01' if applic, then create a date value in the
|
65
|
+
// format YYYY-MM-DD. Save to req.body for processing
|
66
|
+
const preProcess = (req, res, next) => {
|
67
|
+
const parts = getParts(req.body, fields, key);
|
68
|
+
if (_.some(parts, part => part !== '')) {
|
69
|
+
if (dayOptional && parts.day === '') {
|
70
|
+
parts.day = '01';
|
71
|
+
} else {
|
72
|
+
parts.day = pad(parts.day);
|
73
|
+
}
|
74
|
+
if (monthOptional && parts.month === '') {
|
75
|
+
parts.month = '01';
|
76
|
+
} else {
|
77
|
+
parts.month = pad(parts.month);
|
78
|
+
}
|
79
|
+
req.body[key] = `${parts.year}-${parts.month}-${parts.day}`;
|
80
|
+
}
|
81
|
+
next();
|
82
|
+
};
|
83
|
+
|
84
|
+
// defaultFormatters on the base controller replace '--' with '-' on the process step.
|
85
|
+
// This ensures having the correct number of hyphens, so values do not jump from year to month.
|
86
|
+
// This should only be done on a partially completed date field otherwise the validation messages break.
|
87
|
+
const postProcess = (req, res, next) => {
|
88
|
+
const value = req.form.values[key];
|
89
|
+
if (value) {
|
90
|
+
req.form.values[key] = req.body[key];
|
91
|
+
}
|
92
|
+
next();
|
93
|
+
};
|
63
94
|
// if date field is included in errorValues, extend
|
64
95
|
// errorValues with the individual components
|
65
96
|
const preGetErrors = (req, res, next) => {
|
@@ -125,35 +156,15 @@ module.exports = (key, opts) => {
|
|
125
156
|
});
|
126
157
|
};
|
127
158
|
|
128
|
-
// take the 3 date parts, padding or defaulting
|
129
|
-
// to '01' if applic, then create a date value in the
|
130
|
-
// format YYYY-MM-DD. Save to req.body for processing
|
131
|
-
const preProcess = (req, res, next) => {
|
132
|
-
const parts = getParts(req.body, fields, key);
|
133
|
-
if (_.some(parts, part => part !== '')) {
|
134
|
-
if (dayOptional && parts.day === '') {
|
135
|
-
parts.day = '01';
|
136
|
-
} else {
|
137
|
-
parts.day = pad(parts.day);
|
138
|
-
}
|
139
|
-
if (monthOptional && parts.month === '') {
|
140
|
-
parts.month = '01';
|
141
|
-
} else {
|
142
|
-
parts.month = pad(parts.month);
|
143
|
-
}
|
144
|
-
req.body[key] = `${parts.year}-${parts.month}-${parts.day}`;
|
145
|
-
}
|
146
|
-
next();
|
147
|
-
};
|
148
|
-
|
149
159
|
// return config extended with hooks
|
150
160
|
return Object.assign({}, options, {
|
151
161
|
hooks: {
|
162
|
+
'pre-process': preProcess,
|
163
|
+
'post-process': postProcess,
|
152
164
|
'pre-getErrors': preGetErrors,
|
153
165
|
'post-getErrors': postGetErrors,
|
154
166
|
'post-getValues': postGetValues,
|
155
|
-
'pre-render': preRender
|
156
|
-
'pre-process': preProcess
|
167
|
+
'pre-render': preRender
|
157
168
|
}
|
158
169
|
});
|
159
170
|
};
|