I just started a new nodejs app, which will contain a notable amount of forms for creating and editing/updating database values. My question is: How can I repopulate form entries if the given data is invalid?
Example: Somebody tries to login. If the credentials are wrong I'm using something like the following to display the login page again:
res.render('sessions/new')
Thing is, the username is not populated with the request data. I was wondering how to accomplish this both easy and clean. Since there is no form_for
like in rails, I guess I have to implement that manually, right? My first approach was to bypass the request body/params as locals to the render
function and set the value within my jade template like so:
// In controller/route
res.render('sessions/new', req.body)
// In template
input(type='text', name='username', value=locals.username)
Is there a better or more unobtrusive solution? I mean the example above works and is pretty easy to implement, just wondering if I missed something. This is also interesting for stuff like adding an error class to invalid form fields. Couldn't really find anything on the web.
Thanks in advance!