bebraw
4/24/2012 - 6:16 PM

Node form API demo

Node form API demo

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()
        }); 
    }   
};