bug in form behavior?

4 views
Skip to first unread message

mgbe...@gmail.com

unread,
May 14, 2009, 2:02:33 PM5/14/09
to web2py Web Framework
Hello,

This is my first question on the mailing lists, so I must say that I
like web2py a lot!

I've noticed some strange behavior with the form.vars, I'll try to
explain it with a quick example here in an imaginary default.py
controller.

<code>
# defuault.py controller.
def index():
form = FORM(
INPUT(_name='field'),
INPUT(_name='field'),
INPUT(_name='field'),
INPUT(_type='submit'),
)
#####
# So pretend I entered the form with 'John Smith' in the first
input,
# 'Fred Smith' in the second input, and 'Bob Smith' in the third.
#####
if form.accepts(request.vars, session):
return len(form.vars.field)
elif form.errors:
return None
return dict(form=form)
</code>

I would expect form.vars.field to return ['John Smith', 'Fred Smith',
'Bob Smith'] but instead it returns the the representation of the list
as a string, in other words. "['John Smith', 'Fred Smith', 'Bob
Smith']". Therefore, len(form.vars.field) returns 41, whereas you
would expect it to return three.

So my question is, are you suppose to use eval(form.vars.field)
instead, or have I indeed hit on a bug, or am I just confused as to
the usage? Any help is appreciated.

Thanks,
Matt B.

mdipierro

unread,
May 14, 2009, 2:51:34 PM5/14/09
to web2py Web Framework
you may have found a bug. I will look at it asap.

Massimo

DenesL

unread,
May 14, 2009, 6:17:05 PM5/14/09
to web2py Web Framework
Besides the problem in the INPUT class _validate function there is one
more problem here, note that given:

inp=INPUT(_name='field')

then inp.attributes is:

{'type': 'text', 'value' :None, "_name': 'field'}

shouldn't that be '_type' instead of 'type' ?

mdipierro

unread,
May 14, 2009, 10:40:43 PM5/14/09
to web2py Web Framework
Do this instead:
if form.accepts(request.vars, session):
return len(request.vars.field)

I am not sure whether this should be considered a bug since the
process of supporting multiple fields with the same name is only
supported for checkboxes. I will look into this some more anyway.

I strongly suggest giving different names to the three INPUT fields
else you will not be able to reference them using validators (which
why this is not supported).

Massimo


On May 14, 1:02 pm, "mgbeli...@gmail.com" <mgbeli...@gmail.com> wrote:

mgbe...@gmail.com

unread,
May 15, 2009, 12:01:46 AM5/15/09
to web2py Web Framework
Thanks massimo. At your convenience of course. I will usually give
different names, but the identical field names helped me out in this
app https://www.lemurstrikes.com/portfolio/default/sudoku right here.

By the way, I'll be adding my first app to the KPAX app list here
soon.

-Matt B.

DenesL

unread,
May 15, 2009, 10:20:09 AM5/15/09
to web2py Web Framework
@mgbelisle
Nice solvers.

@Massimo
web2py is (correctly IMO) building request.vars.field as a list from
the three INPUTs, so for consistency I would expect form.vars.field to
be an exact copy after accepts. There is nothing in the w3.org specs
against the use of equally named INPUT controls outside the scope of
checkboxes and radio buttons, but you can set it as a web2py
"restriction by design".

My other comment was also geared towards consistency:
inp1=INPUT(_name='field')
inp2=INPUT(_name='field',_type='text') # as used in widgets
inp1.attributes
{'type':'text','value':None,'_name':'field'}
inp2.attributes
{'_type':'text','value':None,'_name':'field'}
I know that you internally manipulate 'type' and '_type' and the logic
has been working so far, but it just seems inconsistent.



mdipierro

unread,
May 15, 2009, 11:37:14 AM5/15/09
to web2py Web Framework
This is not fixed in trunk. Please check. I hope it did not break
anything else.

On May 14, 11:01 pm, "mgbeli...@gmail.com" <mgbeli...@gmail.com>
wrote:
> Thanks massimo.  At your convenience of course.  I will usually give
> different names, but the identical field names helped me out in this
> apphttps://www.lemurstrikes.com/portfolio/default/sudokuright here.

mdipierro

unread,
May 15, 2009, 11:37:47 AM5/15/09
to web2py Web Framework
This is fixed too. Can you please check?
Thanks

Massimo

mgbe...@gmail.com

unread,
May 16, 2009, 11:57:27 AM5/16/09
to web2py Web Framework
Yes, I'm downloading the trunk version now and testing it out. I'll
let you know how it goes Massimo.

mgbe...@gmail.com

unread,
May 16, 2009, 12:53:42 PM5/16/09
to web2py Web Framework
Yes Massimo, it does indeed work in the trunk and I haven't run into
any bad side effects either. Thanks for the speedy fix!

-Matt
Reply all
Reply to author
Forward
0 new messages