diff options
author | Mateja <mail@matejamaric.com> | 2021-05-09 14:41:41 +0200 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2021-05-09 14:41:41 +0200 |
commit | 5ed4787e33e258cc9afa7e9d2886a0b590d69715 (patch) | |
tree | aaad7a8e13ea08b7d548ede3a799d848efb1c730 /utils/middleware | |
parent | 766b60adf81664c59bda39ebc5350af67e325f4e (diff) | |
download | node-playground-5ed4787e33e258cc9afa7e9d2886a0b590d69715.tar.gz node-playground-5ed4787e33e258cc9afa7e9d2886a0b590d69715.zip |
Returning validation errors and old values to `post` form.
Made custom middleware for that purpose.
Diffstat (limited to 'utils/middleware')
-rw-r--r-- | utils/middleware/oldForm.js | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/utils/middleware/oldForm.js b/utils/middleware/oldForm.js new file mode 100644 index 0000000..7bcfb34 --- /dev/null +++ b/utils/middleware/oldForm.js @@ -0,0 +1,23 @@ +module.exports = (req, res, next) => { + + req.setOldForm = (errors) => { + let prettyErrors = {}; + Object.keys(errors.errors).forEach(err => { + prettyErrors[err] = errors.errors[err].message; + }); + + req.session.oldForm = { + errors: prettyErrors, + values: req.body + }; + }; + + if (!req.session.oldForm) req.session.oldForm = { + errors: null, + values: null + }; + + res.locals.oldForm = req.session.oldForm; + delete req.session.oldForm; + next(); +}; |