I am trying to post some data in Koa.js, the post is working fine, but I am struggling to show errors when the form is not filled in correctly. The site needs to work without javascript so I can't guarantee the fields will be correct before submitting (though I have implemented this).
Everytime I redirect back to the form the ctx.request.body and ctx.state are empty.
The only way I've been able to get the data back to the original form is using query params. Fairly new to Koa.js but I've managed to do similar things with Express before.
router.post('/admin/addChapter', (ctx) => {
const body = ctx.request.body;
const expectedKeys = requiredInputs.addChapter;
const hasAll = expectedKeys.every(key => Reflect.has(body, key));
if (hasAll) {
return ctx.render('admin/addChapter', {
seoDescription: 'Some Seo',
pageTitle: 'Add chapter',
titleParam: 'Add chapter',
scripts: ['addChapter/index']
});
} else {
let urlStr = Object.entries(body).map((entry) => entry.join('=')).join('&');
urlStr = urlStr ? `?${urlStr}` : '';
ctx.redirect(`/admin/add${urlStr}`);
return;
}
});