Created
April 24, 2012 18:16
-
-
Save bebraw/2482247 to your computer and use it in GitHub Desktop.
Node form API demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var eventForm = form({ | |
title: type.str({required: true}), | |
location: type.str(), | |
description: type.longStr({required: true}), | |
range: type.dateRange({onlyFuture: true, required: true}), | |
author: type.str({required: true}), | |
email: type.str() // email instead? | |
}); | |
index.get = function(req, res) { | |
res.render('index', { | |
title: 'Create Event', | |
form: eventForm.render(), | |
js: eventForm.js() | |
}); | |
}; | |
index.post = function(req, res) { | |
if(eventForm.isValid(req.body)) { | |
var data = eventForm.clean(req.body); | |
console.log('success', data); | |
// TODO: create event to db and route to new, unique event url | |
} | |
else { | |
res.render('index', { | |
title: 'Not Valid!', // XXX | |
form: eventForm.render(req.body), | |
js: eventForm.js() | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment