Created
June 3, 2011 01:56
-
-
Save criminy/1005722 to your computer and use it in GitHub Desktop.
Form validation example for expressjs
This file contains hidden or 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
server.all('/admin/content/add_article',function(req,res) | |
{ | |
server.form('post',req,res,function(form) { | |
form.required('title') | |
form.optional('tags') | |
form.required('slug') | |
form.required('body') | |
form.valid(function(form) { | |
res.send('Creating blog post titled : ' + form.values.title) | |
}) | |
form.invalid(function(form) { | |
form.render(res,'admin/add_article.html') | |
}) | |
}) | |
}) |
This file contains hidden or 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
/* form validation for expressjs */ | |
function validate(form,ctx) { | |
valid = true | |
for(var idx in form._required_fields) { | |
x = form._required_fields[idx] | |
if(form.values[x] == '') { | |
ctx["validation"][form._name][x] = x + " required" | |
valid = false | |
} | |
ctx[form._name][x] = form.values[x] | |
} | |
return valid | |
} | |
function parse_form(f,req,name){ | |
objs = new Array() | |
for(var x in req.body[name]) { | |
objs.push( x ) | |
} | |
f.values = {} | |
for(var x in req.body[name]) { | |
f.values[x] = req.body[name][x] | |
} | |
} | |
function form(formName, req, res, fn){ | |
o = {} | |
o._name = formName | |
o._required_fields = [] | |
o._optional_fields = [] | |
o.required = function(name){ | |
o._required_fields.push(name) | |
} | |
o.optional = function(name) { | |
o._optional_fields.push(name) | |
} | |
o.render = function(res,template) { | |
ctx = o.ctx | |
res.render(template,ctx) | |
} | |
o.ctx = {} | |
o.valid = function(fn) { o._valid_fn = fn } | |
o.invalid = function(fn) { o._invalid_fn = fn } | |
fn(o) | |
o.ctx["validation"] = {} | |
o.ctx["validation"][formName] = {} | |
o.ctx[formName] = {} | |
for(var x in o._required_fields) { | |
val = o._required_fields[x] | |
o.ctx["validation"][formName][val] = "" | |
o.ctx[formName][val] = "" | |
} | |
for(var x in o._optional_fields) { | |
val = o._optional_fields[x] | |
o.ctx["validation"][formName][val] = "" | |
o.ctx[formName][val] = "" | |
} | |
o.values = {} | |
if(req.method == 'GET') { | |
o._invalid_fn(o) | |
return | |
}else if(req.method == 'POST') {// OR PUT,DELETE, etc | |
parse_form(o,req,o._name) | |
if(validate(o,o.ctx)) { | |
o._valid_fn(o) | |
}else{ | |
o._invalid_fn(o) | |
} | |
} | |
} | |
module.exports = form |
This file contains hidden or 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
<p> | |
<form method="POST" > | |
<p>Title</p> | |
<input type="text" name="post[title]" value="${post.title}"/> | |
${ validation.post.title } | |
<p>Tags</p> | |
<input type="text" name="post[tags]" value="${post.tags}"/> | |
${ validation.post.tags } | |
<p>Slug</p> | |
<input type="text" name="post[slug]" value="${post.slug}"/> | |
${ validation.post.slug } | |
<p>Body ${ validation.post.body } </p> | |
<textarea rows="30" cols="200" name="post[body]">${post.body}</textarea> | |
<br/> | |
<input type="submit" value="Save" name="action"/> | |
<input type="submit" value="Preview" name="action"/> | |
</form> | |
</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment