Bug in web2py?

37 views
Skip to first unread message

Sebastian Bassi

unread,
Aug 6, 2010, 11:38:23 AM8/6/10
to web...@googlegroups.com
I think this feature doesn't work as described:

" If the page contains a single form, you can set formname=None and
omit the hidden field in the view."

Source: http://www.web2py.com/book/default/chapter/07

Because if I do that (using formname=None and omit the hidden field in
the view), the form has errors and enter here as soon as I load it:

elif form.errors:
response.flash = 'form has errors'

But if I put a name in formname and also put the hidden field in the
view, it works without errors.

mdipierro

unread,
Aug 6, 2010, 11:46:37 AM8/6/10
to web2py-users
This is correct. The issue is what is the purpose of the formname? It
is a hidden field used to determine whether the form is being
submitted or not. If you omit formname, web2py cannot determine
whether the form is not submitted or it is submitted with all fields
empty. It assumes the latter and applies validators. It is the
validators that notify you of the errors.

Sebastian Bassi

unread,
Aug 6, 2010, 12:24:19 PM8/6/10
to web...@googlegroups.com
On Fri, Aug 6, 2010 at 12:46 PM, mdipierro <mdip...@cs.depaul.edu> wrote:
> This is correct. The issue is what is the purpose of the formname? It
> is a hidden field used to determine whether the form is being
> submitted or not. If you omit formname, web2py cannot determine
> whether the form is not submitted or it is submitted with all fields
> empty. It assumes the latter and applies validators. It is the
> validators that notify you of the errors.

OK, I see it now.
Is there a way to change how the custom.widget serialize? For example:
{{=form.custom.widget.texto}}
Gets serialized into:
<input class="string" id="sugerencias_texto" name="texto" type="text"
value="" />
But I want a textarea like this:
<textarea name="texto" cols="35" rows="8" id="question_2434"
onKeyUp="counterUpdate('question_2434',
'countBody','8000');"></textarea>
(but also to keep the validation provided by web2py).
Can I do this?

mdipierro

unread,
Aug 6, 2010, 12:36:47 PM8/6/10
to web2py-users
db.table.field.widget=lambda field,value:
SQLFORM.widgets.TextWidget.widget(field,value,cols="35" rows="8")

You can add any attribute you need

On Aug 6, 11:24 am, Sebastian Bassi <sba...@gmail.com> wrote:

Sebastian Bassi

unread,
Aug 6, 2010, 12:50:43 PM8/6/10
to web...@googlegroups.com
On Fri, Aug 6, 2010 at 1:36 PM, mdipierro <mdip...@cs.depaul.edu> wrote:
> db.table.field.widget=lambda field,value:
> SQLFORM.widgets.TextWidget.widget(field,value,cols="35" rows="8")
>
> You can add any attribute you need

I don't see how does it realiz that is a textarea instead of input
type="text" and where should I add this.

mdipierro

unread,
Aug 6, 2010, 1:59:24 PM8/6/10
to web2py-users
StringWidget is the <input type="text"...>
TextWidget is the <textarea ....>

the name comes from the web2py type not the html tag.

On Aug 6, 11:50 am, Sebastian Bassi <sba...@gmail.com> wrote:

Sebastian Bassi

unread,
Aug 6, 2010, 2:29:03 PM8/6/10
to web...@googlegroups.com
On Fri, Aug 6, 2010 at 2:59 PM, mdipierro <mdip...@cs.depaul.edu> wrote:
> StringWidget is the <input type="text"...>
> TextWidget is the <textarea ....>
> the name comes from the web2py type not the html tag.

thank you. Where should I put this and what to put on the view?

mdipierro

unread,
Aug 6, 2010, 3:13:52 PM8/6/10
to web2py-users
You can put it in the model is it should apply to all form for the
table or in the controller action that needs it.

On Aug 6, 1:29 pm, Sebastian Bassi <sba...@gmail.com> wrote:

Sebastian Bassi

unread,
Aug 6, 2010, 3:21:41 PM8/6/10
to web...@googlegroups.com
On Fri, Aug 6, 2010 at 4:13 PM, mdipierro <mdip...@cs.depaul.edu> wrote:
> You can put it in the model is it should apply to all form for the
> table or in the controller action that needs it.

I put it in the controller and I get a syntax error:

Traceback (most recent call last):
File "/home/sbassi/Downloads/web2py/gluon/restricted.py", line 176,
in restricted
ccode = compile2(code,layer)
File "/home/sbassi/Downloads/web2py/gluon/restricted.py", line 163,
in compile2
return compile(code.rstrip().replace('\r\n','\n')+'\n', layer, 'exec')
File "/home/sbassi/Downloads/web2py/applications/sug3/controllers/default.py",
line 13
db.sugerencias.texto.widget=lambda field,value:


SQLFORM.widgets.TextWidget.widget(field,value,cols="35" rows="8")

^
SyntaxError: invalid syntax

Here is the controller code:

def index():
form = SQLFORM(db.sugerencias)
db.sugerencias.texto.widget=lambda field,value:


SQLFORM.widgets.TextWidget.widget(field,value,cols="35" rows="8")

if form.accepts(request.vars, formname='indexform'):
response.flash = 'form accepted'


elif form.errors:
response.flash = 'form has errors'

else:
response.flash = 'please fill the form'
return dict(form=form)

mdipierro

unread,
Aug 6, 2010, 5:24:32 PM8/6/10
to web2py-users
My mistake. Not
form = SQLFORM(db.sugerencias)
db.sugerencias.texto.widget=lambda
field,value:SQLFORM.widgets.TextWidget.widget(field,value,cols="35"
rows="8")

but

db.sugerencias.texto.widget=lambda
field,value:SQLFORM.widgets.text.widget(field,value,cols="35"
rows="8")
form = SQLFORM(db.sugerencias)


On Aug 6, 2:21 pm, Sebastian Bassi <sba...@gmail.com> wrote:

Sebastian Bassi

unread,
Aug 6, 2010, 5:40:06 PM8/6/10
to web...@googlegroups.com, web2py-...@googlegroups.com
On Fri, Aug 6, 2010 at 6:24 PM, mdipierro <mdip...@cs.depaul.edu> wrote:
> My mistake. Not
>    form = SQLFORM(db.sugerencias)
>    db.sugerencias.texto.widget=lambda
> field,value:SQLFORM.widgets.TextWidget.widget(field,value,cols="35"
> rows="8")
> but
>   db.sugerencias.texto.widget=lambda
> field,value:SQLFORM.widgets.text.widget(field,value,cols="35"
> rows="8")
>    form = SQLFORM(db.sugerencias)

Thanks, its getting better!!!

Sebastian Bassi

unread,
Aug 9, 2010, 2:56:56 PM8/9/10
to web...@googlegroups.com
On Fri, Aug 6, 2010 at 1:36 PM, mdipierro <mdip...@cs.depaul.edu> wrote:
> db.table.field.widget=lambda field,value:
> SQLFORM.widgets.TextWidget.widget(field,value,cols="35" rows="8")
>
> You can add any attribute you need

Is this documented somewhere?
I found something strange, with this:

db.sugerencias.texto.widget=lambda
field,value:SQLFORM.widgets.text.widget(field,value,cols="35",rows="8")
form = SQLFORM(db.sugerencias)

I get this:

<textarea class="string" cols="40" id="sugerencias_texto" name="texto"
rows="10"></textarea>

But I was expecting this:

<textarea class="string" cols="35" id="sugerencias_texto" name="texto"
rows="8"></textarea>

Then I tried to add something else and I couldn't. I need to have it like this:

<textarea class="string" cols="35" id="sugerencias_texto" name="texto"
rows="8" onKeyUp="counterUpdate('question_2434',
'countBody','250');"></textarea>

To do this I tried:

db.sugerencias.texto.widget=lambda
field,value:SQLFORM.widgets.text.widget(field,value,cols="35",rows="8",onKeyUp="counterUpdate('question_2434',
'countBody','250');")
form = SQLFORM(db.sugerencias)

But it is ignored and I get:

<textarea class="string" cols="40" id="sugerencias_texto" name="texto"
rows="10"></textarea>

So there are 2 problems, first I get 40 and 10 when I put 35 and 8,
and the 2nd error is that the onKeyUp is ignored :(

mdipierro

unread,
Aug 9, 2010, 5:51:51 PM8/9/10
to web2py-users
we are both missing the _ widgets are helpers.

db.sugerencias.texto.widget=lambda
field,value:SQLFORM.widgets.text.widget(field,value,_cols="35",_rows="8",_onKeyUp="counterUpdate('question_2434','countBody','250');")

This is documented in the book. Chapter 7

On Aug 9, 1:56 pm, Sebastian Bassi <sba...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages