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>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---