Format response for validation errors

This commit is contained in:
Alice Gaudon 2020-06-15 12:58:15 +02:00
parent 44fda787d3
commit 9363873898

View File

@ -71,8 +71,25 @@ export default abstract class Application {
} }
if (err instanceof ValidationBag) { if (err instanceof ValidationBag) {
req.flash('validation', err.getMessages()); res.format({
res.redirectBack(); json: () => {
res.status(401);
res.json({
status: 'error',
code: 401,
message: 'Invalid form data',
messages: err.getMessages(),
});
},
text: () => {
res.status(401);
res.send('Error: ' + err.getMessages())
},
html: () => {
req.flash('validation', err.getMessages());
res.redirectBack();
},
});
return; return;
} }