Prefilling forms?

5 views
Skip to first unread message

Voltron

unread,
Feb 29, 2012, 8:36:52 AM2/29/12
to Humongolus
Using this quick test

class Human(orm.Document):
_db = "test"
_collection = "humans"
human_id = field.AutoIncrement(collection="human")
name = field.Char(required=True, min=2, max=25)

class PersonForm(widget.Form):
_action = '/save_person'
_fields = ["human_id", "name"]
_id = "css_id"
_class= "css_class"

human_id = widget.Input(label="ID")
name = widget.Input(label="Name")


submit = {
"name":"Megatron",
"human_id":"6789",
}

human =Human()
human.name = "voltron"
human.human_id = "1234"


empty_form = PersonForm()
obj_form = PersonForm(object=human)
data_form = PersonForm(data=submit)
form = PersonForm(object=human, data=submit)

print
print "empty_form: ", empty_form.render()
print

print "obj_form: ", obj_form.render()
print

print "data_form: ", data_form.render()
print

print "form: ", form.render()


try:
form.validate()
except orm.DocumentException as e:
for f in form:
if f.errors:
print f.name
if f.description: print f.description
print f.errors

print form.errors
print e.errors
else:
for f in form:
print f.render()


The result is:

empty_form: <form id='css_id' class='' name='' action='/save_person'
method='POST' type='multipart/form'><label for='human_id'>ID</
label><input type='text' id='id_human_id' name='human_id' value='None'
class='' /><label for='name'>Name</label><input type='text'
id='id_name' name='name' value='None' class='' /><input type='submit'
value='submit' /></form>

obj_form: <form id='css_id' class='' name='' action='/save_person'
method='POST' type='multipart/form'><label for='human_id'>ID</
label><input type='text' id='id_human_id' name='human_id' value='1234'
class='' /><label for='name'>Name</label><input type='text'
id='id_name' name='name' value='voltron' class='' /><input
type='submit' value='submit' /></form>

data_form: <form id='css_id' class='' name='' action='/save_person'
method='POST' type='multipart/form'><label for='human_id'>ID</
label><input type='text' id='id_human_id' name='human_id' value='None'
class='' /><label for='name'>Name</label><input type='text'
id='id_name' name='name' value='None' class='' /><input type='submit'
value='submit' /></form>

form: <form id='css_id' class='' name='' action='/save_person'
method='POST' type='multipart/form'><label for='human_id'>ID</
label><input type='text' id='id_human_id' name='human_id' value='1234'
class='' /><label for='name'>Name</label><input type='text'
id='id_name' name='name' value='voltron' class='' /><input
type='submit' value='submit' /></form>
<input type='text' id='id_human_id' name='human_id' value='6789'
class='' />
<input type='text' id='id_name' name='name' value='Megatron'
class='' />

So can I infer that the correct way to pre-fill a form is by using an
instantiated object and not just by passing data to the data attribute
of a form?

Christopher Coté

unread,
Feb 29, 2012, 11:16:25 AM2/29/12
to humon...@googlegroups.com

If you pass an empty object with your data it should work. Theres no way to really validat the fields without the actual types.

Voltron

unread,
Feb 29, 2012, 7:01:30 PM2/29/12
to Humongolus
That works. Thanks
Reply all
Reply to author
Forward
0 new messages