freewil / express-form

Form validation and data filtering for Express
MIT License
89 stars 21 forks source link

trying to use express-form in a function (not inline) #1

Open warnero opened 11 years ago

warnero commented 11 years ago

I have this defined in an external file exports.validateObject = function(req) { return form( form.field("bookmark_url").trim().required().isUrl(), form.field("bookmark_title").trim(), form.field("bookmark_tags").trim(), form.field("bookmark_description").trim() ); }

I'm then using it in my app.js like this: app.post('/notebooks/:notebookId/bookmark', route.validateObject, route.saveObject);

It gets to the validateObject function but never calls my route method. Hoping you can help shed some light on how to use it this way as I don't see any examples that allow you to separate your validation from where you define your routing. I have also verified that all the fields are coming through to the validateObject method by looking at the req.body.

warnero commented 11 years ago

Friend helped me out and I managed to fix it by doing this instead exports.validateObject = form( form.field("bookmark_url").trim().required().isUrl(), form.field("bookmark_title").trim(), form.field("bookmark_tags").trim(), form.field("bookmark_description").trim() );

Would be good if you could add an example showing this for others.

freewil commented 11 years ago

Good suggestion, I had this issue myself when I tried to do the same thing.

sbabigian commented 11 years ago

:+1: I just hit this today. So glad to have found warnero's snippet. Which in hindsight seems so obvious, thanks!

schinery commented 10 years ago

Glad I stumbled upon this! Nice one...