Custom forms and dealing with error messages

71 views
Skip to first unread message

jjah...@googlemail.com

unread,
Jun 28, 2009, 3:39:53 AM6/28/09
to web2py Web Framework
I have been playing around with web2py to see what I can do or not do.

I must say I like what I see. It seems very intuitive and easy to work
with.

I am using windows XP SP3, python 2.5.4, web2py 1.64.4

It would seem that the default format for forms is to output them as
tables.

So I have looked at using Custom Forms but continuing to use the
validation system. I have picked up examples from various messages and
modified them to see what I can do.

The custom forms work with the simple layout I have been using for my
tests. But the one thing I have become stuck on at the moment is how I
can change the position of error messages.

I have included the code I have been using. You can see how I have
played around with using my own form views and adding form elements
and widgets myself to see what happens. If you submit the form, it is
validated and the error messages appear attached to each form item.

Ideally I would like to control where the error messages appear. But I
have become brain dead. I must be overlooking something obvious. So I
am checking to see if anyone can give me a pointer to what I can do.

One other point is that by default every element comes with a class
name attached. I could just update each element and set the class to
None to get rid of it. And if I need to I could then add my own class
names. But from the point of view of keeping the form tidy, would it
be useful to be able not to have default class names attached. Then I
can avoid having to set each element off.

Here is the code. I hope the formatting stays good so you can read it.
The model just has some menu code so I can see what I can do with
menus. It works, but not sure if I have put it in the right place
holder. I must admit I have not found too much info on menus.

Model

try:
from gluon.contrib.gql import * # if running on Google App Engine
except:
db = SQLDB('sqlite://storage.db') # if not, use SQLite or other
DB
else:
db = GQLDB() # connect to Google BigTable
session.connect(request, response, db=db) # and store sessions
there
# or use the following lines to store sessions in Memcache
#from gluon.contrib.memdb import MEMDB
#from google.appengine.api.memcache import Client
#session.connect(request, response, db=MEMDB(Client()))


response.menu = [
['Index', False,
URL(request.application,'default','index')],
['another', False,
URL(request.application,'default','another')],
]

Controller

# # sample index page with internationalization (T)
def index():
response.flash = T('Welcome to web2py')
return dict(message=T('Hello World'))


def form_factory(*a): return SQLFORM(SQLDB(None).define_table(*a))

#and now you can create a controller like the following


def formtest2():
options = ['Mr','Dr','Mrs','Ms']
options2 = ['New York','Boston','Baltimore']
form=form_factory('myform',
SQLField('name','string',requires=IS_NOT_EMPTY()),
SQLField('lastname','string',requires=IS_NOT_EMPTY()),
SQLField('address1','string',requires=IS_NOT_EMPTY()),
SQLField('address2','string',requires=IS_NOT_EMPTY()),
SQLField('test',requires=IS_IN_SET(options)),
SQLField('city',requires=IS_IN_SET(options2)),
SQLField('zip','string',requires=IS_NOT_EMPTY()))

if form.accepts(request.vars,session):
response.flash='formc accepted'
### do something with form.vars, perhaps redirect somewhere
else
elif form.errors:
response.flash='errors in formc'
else:
response.flash='please fill the formc again'
return dict(form=form, vars=form.vars)

View

{{extend 'layout.html'}}
<h1>This is the default/formtest2.html template</h1>
<title>Layout Form without Tables</title>
<style type="text/css">
.formLayout
{
background-color: #f3f3f3;
border: solid 1px #a1a1a1;
padding: 10px;
width: 300px;
}

.formLayout label, .formLayout input
{
display: block;
width: 120px;
float: left;
margin-bottom: 10px;
}

.formLayout label
{
text-align: right;
padding-right: 20px;
}

br
{
clear: left;
}
</style>
</head>
<body>

<form action="" enctype="multipart/form-data" method="post">
<div class="formLayout">
<h2>Contact Form</h2><br>
<label>Title</label>
{{=form.element(_name="test")}}<br>
<label>First Name</label>
{{=form.element(_name="name")}}<br>
<label>Last Name</label>
{{=form.element(_name="lastname")}}<br>
<label>Address</label>
{{form.element(_name="address1").update(_class=None)}}
{{=form.custom.widget.address1}}<br>
<label></label>
{{=form.element(_name="address2")}}<br>
<label>City</label>
{{=form.element(_name="city")}}<br>
<label>Zip</label>
{{=form.element(_name="zip")}}<br>
<div style="padding-left:7em">
<input type="submit" value="submit" ><br></div>
</div>
CUSTOM
{{=form.custom}}FIELDS
{{=form.fields}}
ERRORS
{{=form.errors}}
LAST
{{=form.custom.dspval.address1}}
{{=form.custom.inpval.address1}}<br>
{{=form.hidden_fields()}}
</form>
</body>
</html>

End of Code

I hope someone can give me a few pointers, or point out any obvious
mistakes.

Regards

John Aherne

mdipierro

unread,
Jun 28, 2009, 11:01:14 AM6/28/09
to web2py Web Framework
Actually the best way to customize forms is using the method explain
in slide 72 (slides are not at www.web2py.com).

Error messages normally are in form.errors and they get displayed
below the corresponding widgets but you can do the following in views
for example:

{{(errors,form.errors)=(form.errors,dict())}}

so that errors are not displayed under widgets any more but stored in
a new "errors" dictionary. And you can display them as a you like:

{{if 'name' in errors:}}Oops, there is no name here{{pass}}


On Jun 28, 2:39 am, "jjahe...@googlemail.com"

jjah...@googlemail.com

unread,
Jun 28, 2009, 4:29:20 PM6/28/09
to web2py Web Framework
Thanks for the reply. I'll try it out and see where I get to.

Regards

John Aherne

John Aherne

unread,
Jun 29, 2009, 3:53:26 AM6/29/09
to web2py Web Framework
I still seem to be missing something. Must be obvious but I can't see it.

I use the example code you showed, and the errors dict shows the values and form.errors is now an empty dict, but the error messages still get displayed in the old manner without me doing anything. It's as if the error messages are displayed from somewhere else, but I can't see where.

I put this line of code towards the top of my view:--

{{(errors,form.errors)=(form.errors,dict())}}

Then I display errors and form.errors to make sure they have what I expect. Form.errors is empty. Now I would expect not to see any errror messages unless I place them myself. Unfortunately, they still appear in the old position as though I had made no changes.

What have I overlooked.

John Aherne

Archer

unread,
Jul 5, 2009, 5:56:16 PM7/5/09
to web2py Web Framework
Ah - slide 72? Where would that be?

On Jun 28, 8:01 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Actually the best way to customize forms is using the method explain
> in slide 72 (slides are not atwww.web2py.com).
>
> Errormessages normally are inform.errors and they get displayed
> below the corresponding widgets but you can do the following in views
> for example:
>
> {{(errors,form.errors)=(form.errors,dict())}}
>
> so that errors are not displayed under widgets any more but stored in
> a new "errors" dictionary. And you can display them as a you like:
>
> {{if 'name' in errors:}}Oops, there is no name here{{pass}}
>
> On Jun 28, 2:39 am, "jjahe...@googlemail.com"
>
> <jjahe...@googlemail.com> wrote:
> > I have been playing around with web2py to see what I can do or not do.
>
> > I must say I like what I see. It seems very intuitive and easy to work
> > with.
>
> > I am using windows XP SP3, python 2.5.4, web2py 1.64.4
>
> > It would seem that the default format for forms is to output them as
> > tables.
>
> > So I have looked at usingCustomForms but continuing to use the
> > validation system. I have picked up examples from various messages and
> > modified them to see what I can do.
>
> > Thecustomforms work with the simple layout I have been using for my
> > tests. But the one thing I have become stuck on at the moment is how I
> > can change the position oferrormessages.
>
> > I have included the code I have been using. You can see how I have
> > played around with using my ownformviews and addingformelements
> > and widgets myself to see what happens. If you submit theform, it is
> > validated and theerrormessages appear attached to eachformitem.
>
> > Ideally I would like to control where theerrormessages appear. But I
> > have become brain dead. I must be overlooking something obvious. So I
> > am checking to see if anyone can give me a pointer to what I can do.
>
> > One other point is that by default every element comes with a class
> > name attached. I could just update each element and set the class to
> > None to get rid of it. And if I need to I could then add my own class
> > names. But from the point of view of keeping theformtidy, would it
> >     <title>LayoutFormwithout Tables</title>
> >     <style type="text/css">
> >     .formLayout
> >     {
> >         background-color: #f3f3f3;
> >         border: solid 1px #a1a1a1;
> >         padding: 10px;
> >         width: 300px;
> >     }
>
> >     .formLayout label, .formLayout input
> >     {
> >         display: block;
> >         width: 120px;
> >         float: left;
> >         margin-bottom: 10px;
> >     }
>
> >     .formLayout label
> >     {
> >         text-align: right;
> >         padding-right: 20px;
> >     }
>
> >     br
> >     {
> >         clear: left;
> >     }
> >     </style>
> > </head>
> > <body>
>
> > <formaction="" enctype="multipart/form-data" method="post">
> >     <div class="formLayout">
> >     <h2>ContactForm</h2><br>

Yarko Tymciurak

unread,
Jul 5, 2009, 6:02:26 PM7/5/09
to web...@googlegroups.com
On Sun, Jul 5, 2009 at 4:56 PM, Archer <dfdum...@gmail.com> wrote:

Ah - slide 72? Where would that be?

On Jun 28, 8:01 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Actually the best way to customize forms is using the method explain
> in slide 72 (slides are not atwww.web2py.com).

I think this meant to say "slides are _now at http://www.web2py.com"   - you will need flash on your browser to see them; you can also download them from the link (but need to register).
 

mdipierro

unread,
Jul 5, 2009, 6:19:26 PM7/5/09
to web2py Web Framework
you can also download them directly from web2py.com/examples/default/
docs in pdf/ppt/key bypassing scribd

On Jul 5, 5:02 pm, Yarko Tymciurak <yark...@gmail.com> wrote:
> On Sun, Jul 5, 2009 at 4:56 PM, Archer <dfdumar...@gmail.com> wrote:
>
> > Ah - slide 72? Where would that be?
>
> > On Jun 28, 8:01 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> > > Actually the best way to customize forms is using the method explain
> > > in slide 72 (slides are not atwww.web2py.com).
>
> I think this meant to say "slides are _now athttp://www.web2py.com"   - you
Reply all
Reply to author
Forward
0 new messages