From 5a4d97b1bdb87c9b0daa269d1c9c0be10b6e94db Mon Sep 17 00:00:00 2001 From: Hippo Date: Wed, 5 Jan 2022 19:36:13 +0530 Subject: [PATCH] Validate submit button in view (not in validation function) This is so the validation function can also be used in other places, such as while processing an already saved pledge. --- server/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/server/index.js b/server/index.js index a0b5d4d..c7d7202 100644 --- a/server/index.js +++ b/server/index.js @@ -123,11 +123,6 @@ function validatePledge(body, PledgeModel = Pledge) { // errors get saved here let errors = [] - let submit = body.submit - if (submit != 'Save Pledge') { - errors.push("This request seems to have been tampered with. Are you sure it wasn't you doing the tampering?") - } - let robo = body.robo if(robo != 'no') { errors.push('Only humans are allowed to donate money. Robots are too digital 🙁') @@ -194,6 +189,12 @@ function validatePledge(body, PledgeModel = Pledge) { router.post('/pledge', async (req, res) => { if (DEBUG) console.debug('New pledge:', req.body) + // check that the right submit button was pressed + let submit = req.body.submit + if (submit != 'Save Pledge') { + errors.push("This request seems to have been tampered with. Are you sure it wasn't you doing the tampering?") + } + // process the pledge with our handy function let {pledge, errors} = validatePledge(req.body, UnverifiedPledge)