Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

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

Open
warnero opened this issue May 1, 2013 · 4 comments
Open

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

warnero opened this issue May 1, 2013 · 4 comments
Labels

Comments

@warnero
Copy link

warnero commented May 1, 2013

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
Copy link
Author

warnero commented May 1, 2013

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
Copy link
Owner

freewil commented May 4, 2013

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

@sbabigian
Copy link

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

@schinery
Copy link

Glad I stumbled upon this! Nice one...

@freewil freewil added the docs label Jan 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants