iterating through form fields with rows

104 views
Skip to first unread message

aetag...@gmail.com

unread,
Mar 15, 2016, 4:22:45 PM3/15/16
to web2py-users
I have a table defined with 3 input fields and then an upload field. The upload field is used to take the values that are entered by the user and input them into a file which contains all form field values from the session.

The form is generated dynamically, so the number of form fields varies with each session. 

I am stuck on figuring out how to capture the individual form field values. I am unsure if maybe I should be reading these into an array and then populating the file? 


If the form that is generated contains only one instance of the form fields, I can grab these values by doing row.field1, row.field2, row.field3.

I was wondering if web2py contains a method for dealing with the multiple instances of form fields?
For example, if the user created two instances of the form fields I would capture values like this:
row.field1_0, row.field2_0, row.field3_0
row.field1_1, row.field2_1, row.field2_1

However this does not work..
I have tried doing variations of the following:

for i in row.field1:
    print row.field1


When I run this with a session containing the form with two instances of form fields it will show me the values that were inputted for that session in field1_0, as well as field1_1. So I am able to at least see that it is capturing the separate entries in the single form. I am just not sure how can I can iterate through the form fields or if there is a keyword or method within web2py which will allow me to do this so I can place them accordingly before the file is uploaded.

Richard Vézina

unread,
Mar 15, 2016, 4:38:51 PM3/15/16
to web2py-users
for i in range(0, number_of_total_fields):
    row[field+i]

Richard

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Val K

unread,
Mar 15, 2016, 5:15:12 PM3/15/16
to web...@googlegroups.com
Hi!
Just for clarity: 
row.field_name - access to field value of record retrieved from DB ( by db().select() ) 
form.vars.field_name - access to field value of submitted form (  after form.process() )

form.vars - just a Storage() object  like a dict()

form.vars.keys() - list of all field_names
form.vars.values() - list of all field_values

aetag...@gmail.com

unread,
Mar 24, 2016, 4:52:06 PM3/24/16
to web2py-users
What worked for me was utilizing row.values(), because I was doing this within the models file!
I haven't been able to find any documentation in regards to using it but that is what I was looking for! Thank you!
Reply all
Reply to author
Forward
0 new messages