Custom forms with SQLFORM

43 views
Skip to first unread message

DenesL

unread,
Feb 16, 2009, 12:27:23 AM2/16/09
to web2py Web Framework
Starting with rev.726 SQLFORM provides new attributes useful to build
custom forms without the need to re-access the DB.

For more information please read Alter Ego entry 205:
https://mdp.cti.depaul.edu/AlterEgo/default/show/205

Comments welcome.
DenesL.

mdipierro

unread,
Feb 16, 2009, 8:27:35 AM2/16/09
to web2py Web Framework
Thank you Denes.

Massimo

murray3

unread,
Feb 17, 2009, 6:07:52 AM2/17/09
to web2py Web Framework
using the famous mywiki as my testbed i could do with a little more
direction
with implementing the new custom sqlforms:
in model:
db.define_table('page',
SQLField('timestamp','datetime',default=now),
SQLField('title'),
SQLField('body','text'))

in controller:
def edit():
"edit for for existing wiki page"
try: thispage=db(db.page.id==request.args[0]).select()[0]
except: redirect(URL(r=request,f='index'))
myform=SQLFORM(db.page,thispage,deletable=False,fields=['body'])
if myform.accepts(request.vars,session):
session.flash='page saved'
redirect(URL(r=request,f='show',args=request.args))
elif myform.errors: response.flash='page not saved'
return dict(form=myform)

in view:
??

I only want to show the body as a textarea to use nicedit with it then
I plan
to save back to db using {{=XML(record.body,sanitize=True)}} format.

thanks
chrism

DenesL

unread,
Feb 17, 2009, 9:53:37 AM2/17/09
to web2py Web Framework
On Feb 17, 6:07 am, murray3 <ch...@murraypost.net> wrote:
> using the famous mywiki as my testbed i could do with a little more
> direction with implementing the new custom sqlforms:
...
> I only want to show the body as a textarea to use nicedit with it

Hi chrism, in view you can use:
...
<form>
<textarea name="body">
{{=form.custom.dspval.body}}
</textarea>
</form>
...

and add any HTML (or other) code around it.

murray3

unread,
Feb 17, 2009, 5:05:57 PM2/17/09
to web2py Web Framework
DenesL.
thanks for help, but I am still having prob's with my code.
as previous mywiki example
in view:
{{dsp=form.custom.dspval}}
{{inp=form.custom.inpval}}
{{lbl=form.custom.label}}
...
<form action=" {{=XML(inp.body,sanitize=True)}} " method="post">
<textarea style="width: 99%; align: center; height:100px;background:
snow;" id="wikibody1" name="data"name="wikibody1">
{{=form.custom.dspval.body}}
</textarea>
<input type="submit" value="Submit"/>
</form>

just clears the form and does not save to db, sorry I am a little slow
but I am trying to learn this stuff (slowly), this is a hobby:) I just
want to save my nicedit textarea data to the db
and I wanted a larger text area than SQLFORM() default.
thanks
chris

DenesL

unread,
Feb 17, 2009, 5:54:50 PM2/17/09
to web2py Web Framework
No worries chris, I will try to give you a hand.

On Feb 17, 5:05 pm, murray3 <ch...@murraypost.net> wrote:
> DenesL.
> thanks for help, but I am still having prob's with my code.
> as previous mywiki example
> in view:
> {{dsp=form.custom.dspval}}
> {{inp=form.custom.inpval}}
> {{lbl=form.custom.label}}
> ...
> <form action=" {{=XML(inp.body,sanitize=True)}} " method="post">

action should be empty for self-submission,
and it does not hurt to add encoding type,
so your form should start with:
<form action="" method="post" enctype="application/x-www-form-
urlencoded">

> <textarea style="width: 99%; align: center; height:100px;background:
> snow;" id="wikibody1" name="data"name="wikibody1">

every element returned by a form should have a name (and only one
name) identical to your database field, so your textarea should have
name="body"
I would recommend downloading the very handy HTML 4 reference
available in different formats from
http://htmlhelp.com/reference/html40/

> {{=form.custom.dspval.body}}

this is fine but if you have
{{dsp=form.custom.dspval}}
at the top (as you do) this can shortened to
{{=dsp.body}}
the idea is to type less :) specially in long, complex forms

> </textarea>
> <input type="submit" value="Submit"/>
> </form>
>
> just clears the form and does not save to db, sorry I am a little slow
> but I am trying to learn this stuff (slowly), this is a hobby:) I just
> want to save my nicedit textarea data to the db
> and I wanted a larger text area than SQLFORM() default.

I have not used nicedit so you must read up on it and see what is
required to use it.
As for saving the textarea to the DB that is done automatically in
web2py during form.accepts

> thanks
> chris

Hope this helps.
Denes.

DenesL

unread,
Feb 18, 2009, 6:14:09 AM2/18/09
to web2py Web Framework
Note that
<form action="" method="post" enctype="application/x-www-form-
urlencoded">

should be
<form action="" method="post" enctype="multipart/form-data">

if you have INPUT elements with type="file" as per
http://www.w3.org/TR/html401/interact/forms.html#adef-enctype

so it is safer to use the second version.

murray3

unread,
Feb 21, 2009, 7:58:54 PM2/21/09
to web2py Web Framework
thanks for the help on this, but I didn't get it to work and I have
been preocupied
with designing a railway(day job:).
I am still using standard SQLFORM object as this allows for the
nicEdit markup to be
saved to db. When I try using custom form it does not submit.

All I wanted todo was use a textbox with a style = " width: 100%;"
instead of usual
sqlform style which is too narrow.
It would be usefull if you guy's did a complete run through of mvc
using the mywiki
type app to show custom form syntax, and how it is possible to
implement different
types of HTML form elements (i.e. textarea etc).

thanks
chrism

On Feb 18, 11:14 am, DenesL <denes1...@yahoo.ca> wrote:
> Note that
> <form action="" method="post" enctype="application/x-www-form-
> urlencoded">
>
> should be
> <form action="" method="post" enctype="multipart/form-data">
>
> if you have INPUT elements with type="file" as perhttp://www.w3.org/TR/html401/interact/forms.html#adef-enctype
Reply all
Reply to author
Forward
0 new messages