newbie looking for style advice

13 views
Skip to first unread message

devnull

unread,
Oct 5, 2009, 11:14:06 AM10/5/09
to web2py-users
Hello all. Just discovered web2py and I think it's great. I wish we
were using it here at work, and so do some of my co-workers.

I have created a little form as an exercise for myself and I came up
with some questions.

It's likely that some of the things I'm wondering about are handled by
the framework and I just don't know it yet...

... and the rest of the stuff is probably telltale stylistic noob-ness
that I hope you'll point out.

At the bottom is the controller and view for my form. There's no model
yet. The form just adds two values, x and y. Here are the questions.

1. The basic structure of the form is defined in the controller part.
I did this so I could use the 'requires' argument to the INPUT method
to specify a validator. Is this ok? The reason I ask is it feels like
I'm putting view stuff in the controller when I say things like TABLE
and TR...

2. I wanted a more specific error message for each field which uses an
is-float-in-range validator ... But is there a way to refer to the
actual max and min without repeating it in the string? Something like
$max and $min?

3. At the end of the controller, am I making a mistake by passing x
and y explicitly? Are they also hiding inside form and can I extract x
and y from the form in the view html?

4. Would it be a better practice to redirect the user to another page
for the result instead of doing everything on the same page? I tried
this but had some difficulty getting x and y in the second page for
processing (adding).

5. Do you have any other advice?

Looking forward to learning from you all. Thanks in advance!

controller:

def form_example():
form=FORM(TABLE(
TR('x',
INPUT(_type='text',_name='x',
requires=IS_FLOAT_IN_RANGE(
0,500,
error_message='number between 0 and 500
required'))),
TR('y',
INPUT(_type='text',_name='y',
requires=IS_FLOAT_IN_RANGE(
0,100,
error_message='number between 0 and 100
required'))),
TR('',INPUT(_type='submit',
_value='add'))))
sum = x = y = ''
visibility = 'hidden'
if form.accepts(request.vars, session):
sum = float(request.vars.x) + float(request.vars.y)
visibility = 'visible'
return dict(form = form,
sum = sum,
x = request.vars.x,
y = request.vars.y,
visibility = visibility)

view:

<h1>form example</h1>
{{=form}}
<table style="visibility:{{=visibility}}">
<tr>
<td>Most recent x entered</td>
<td>{{=x}}</td>
</tr>
<tr>
<td>Most recent y entered</td>
<td>{{=y}}</td>
</tr>
<tr>
<td>sum</td>
<td>{{=sum}}</td>
</tr>
</table>

mdipierro

unread,
Oct 5, 2009, 11:31:59 AM10/5/09
to web2py-users
Thank you devnull,

some answers below:

On Oct 5, 10:14 am, devnull <ache...@gmail.com> wrote:
> Hello all. Just discovered web2py and I think it's great. I wish we
> were using it here at work, and so do some of my co-workers.
>
> I have created a little form as an exercise for myself and I came up
> with some questions.
>
> It's likely that some of the things I'm wondering about are handled by
> the framework and I just don't know it yet...
>
> ... and the rest of the stuff is probably telltale stylistic noob-ness
> that I hope you'll point out.
>
> At the bottom is the controller and view for my form. There's no model
> yet. The form just adds two values, x and y. Here are the questions.
>
> 1. The basic structure of the form is defined in the controller part.
> I did this so I could use the 'requires' argument to the INPUT method
> to specify a validator. Is this ok? The reason I ask is it feels like
> I'm putting view stuff in the controller when I say things like TABLE
> and TR...


You can do
form=SQLFORM.factory(Field('x',double')),Field('y','double')))

and in view
{{=form.custom.begin}}
{{=form.custom.widget.x}}
{{=form.custom.widget.y}}
{{=form.custom.submit}}
{{=form.custom.end}}
then insert the HTML you need in the view.

> 2. I wanted a more specific error message for each field which uses an
> is-float-in-range validator ... But is there a way to refer to the
> actual max and min without repeating it in the string? Something like
> $max and $min?

No, there is not. We could add it. If we do it would be %{minimum}s %
(maximum)s. Pros/Cons?

> 3. At the end of the controller, am I making a mistake by passing x
> and y explicitly? Are they also hiding inside form and can I extract x
> and y from the form in the view html?

You are not but form.vars.x is available inside the view since you are
passing the form.

> 4. Would it be a better practice to redirect the user to another page
> for the result instead of doing everything on the same page? I tried
> this but had some difficulty getting x and y in the second page for
> processing (adding).

I like to redirect on accept. In this case there is no different but
you may have multiple objects in one page that depend on one another.
Without an explicit redirection it ma be difficult to keep track of
their state.

> 5. Do you have any other advice?

No. It seems to me you know aht you are doing.

devnull

unread,
Oct 5, 2009, 2:26:40 PM10/5/09
to web2py-users
Below is the difficulty I was having getting request.vars to the
target of the redirect. [add] redirects to [add_2], and when it does,
request.vars doesn't have .x and .y ... instead I find myself stuffing
x and y into the session explicitly and then pulling them out in
[add_2] also explicitly.

Is there something in the framework that handles this? (I feel like
I'm missing something only a newbie would miss). :-) .... maybe
[redirect] or [URL] takes another argument that I don't know about
yet... I hope I've freed you from the notion that I know what I'm
doing :-P

def add():
form=SQLFORM.factory(
Field('x','double',
requires=IS_FLOAT_IN_RANGE(
0,500,
error_message='number between 0 and 500 required')),
Field('y','double',
requires=IS_FLOAT_IN_RANGE(
0,100,
error_message='number between 0 and 100 required')))
if form.accepts(request.vars, session):
session.x = request.vars.x
session.y = request.vars.y
redirect(URL(r=request,f='add_2'))
return dict(form=form)

def add_2():
x=session.x
y=session.y
sum=float(x)+float(y)
return dict(x=x,y=y,sum=sum)

Richard

unread,
Oct 6, 2009, 12:15:32 AM10/6/09
to web2py-users
when you redirect you get a new request object so you could pass the
data to the new request.vars with:
URL(r=request, f='add_2', vars={'x':request.vars.x,
'y':request.vars.y})
or simply:
URL(r=request, f='add_2', vars=request.vars)

Probably better to pass temporary data in vars than store in session.

Richard

Iceberg

unread,
Oct 6, 2009, 6:48:50 AM10/6/09
to web2py-users
On Oct5, 11:31pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> On Oct 5, 10:14 am, devnull <ache...@gmail.com> wrote:
> > 2. I wanted a more specific error message for each field which uses an
> > is-float-in-range validator ... But is there a way to refer to the
> > actual max and min without repeating it in the string? Something like
> > $max and $min?
>
> No, there is not. We could add it. If we do it would be %{minimum}s %
> (maximum)s. Pros/Cons?
>

It seems a good idea without cons.

Besides, this time please make the default error_message of EACH
validator easier to customize. For example,

class IS_FLOAT_IN_RANGE(Validator):
error_message = 'input between %(minimum)s and %(maximum)s'
def __init__(self, ..., error_message=None):
if error_message is None:
error_message = self.error_message

So that later we could customize the express style for all the SAME
kinds of validator, by:
IS_FLOAT_IN_RANGE.error_messsage = '''
OH NO~~, YOU MUST INPUT BETWEEN %(minimum)s AND %(maximum)s'''
db.mytable.myfield1.requires = IS_FLOAT_IN_RANGE(0,100)
db.mytable.myfield2.requires = IS_FLOAT_IN_RANGE(3,50)
...
Reply all
Reply to author
Forward
0 new messages