[web2py] Re: Empty HTML input array issue

2011-09-28 Thread Anthony
You've given the same name to three fields ('item[]'). web2py treats that as 
one field, so it puts all the values submitted in those fields into a single 
list to represent the value of the field. When an error is returned, web2py 
populates the form with the previously submitted values, so it is populating 
each of the item[] fields with the list of values submitted. Since there are 
three fields, it should actually be ['','',''] (the empty quotes would be 
replaced with actual values if any items were submitted with the form).

Is the 'items[]' field a list:string field? Why does the name have '[]' at 
the end of it? What does your DB model look like?

Anthony

On Wednesday, September 28, 2011 1:18:35 AM UTC-4, Joseph Jude wrote:

 Hi all,
 I have the below form (leaving out non-essential code)

 form = FORM(
 DIV(
 UL(
 SPAN(LABEL('Name:')),
 SPAN(INPUT(_type=text, _name=name, requires=IS_NOT_EMPTY(,
 DIV(
 LABEL(Items:),
 UL(
 SPAN(INPUT(_type=text, _name=items[])), 
 SPAN(INPUT(_type=text, _name=items[])), 
 SPAN(INPUT(_type=text, _name=items[]))
 ))
 DIV(
 INPUT(_type=submit, _value=Create My List),
 _class=submitbtn button)
 ))
 if form.accepts(request.vars, session):
 rest of code
 If this form is submitted with values the form values are written into the 
 databases. No problem. However, if it is submitted with an empty first field 
 (name), the the fields _name=items[] are filled with values ['', ''] when 
 errors are flashed on the screen. I am not able to find why it happens and 
 also how to fix it. (there is a reason why I use the _name=items[] - user 
 should be able to add as many items as possible at the client side). Any 
 clues and solution is appreciated. Thank you.
 Joseph



[web2py] Re: Empty HTML input array issue

2011-09-28 Thread Joseph Jude
Thanks Anthony for the reply.

1) With your answer, I understand why the ['',''] is displayed. But is there 
a way to change it?
2) [] is appended since I wanted an array. But in the mean-while I 
experimented without [] and just having 'items' as the variable name for all 
the three fields and got the same behavior (your explanation tells me why).
3) I want the user to add as many fields as needed. Think of list of to-do 
items for a single topic.
4) the db model is not a list:string. it is a regular table:

db.define_table('checklist_items',
Field('checklist_id', db.checklists),
Field('item_name')
)

5) there is only one field in the form which is mandatory; others (about 5 
other fields) are not. so I can't use keepvalues=false

Hope to hear.
Thanks again,
Joseph


[web2py] Re: Empty HTML input array issue

2011-09-28 Thread Anthony
That's tricky. You might get ideas from 
this: https://groups.google.com/d/topic/web2py/UK8NZ1VMlNk/discussion. Maybe 
others have suggestions.

Anthony

On Wednesday, September 28, 2011 9:48:16 AM UTC-4, Joseph Jude wrote:

 Thanks Anthony for the reply.

 1) With your answer, I understand why the ['',''] is displayed. But is 
 there a way to change it?
 2) [] is appended since I wanted an array. But in the mean-while I 
 experimented without [] and just having 'items' as the variable name for all 
 the three fields and got the same behavior (your explanation tells me why).
 3) I want the user to add as many fields as needed. Think of list of to-do 
 items for a single topic.
 4) the db model is not a list:string. it is a regular table:

 db.define_table('checklist_items',
 Field('checklist_id', db.checklists),
 Field('item_name')
 )

 5) there is only one field in the form which is mandatory; others (about 5 
 other fields) are not. so I can't use keepvalues=false

 Hope to hear.
 Thanks again,
 Joseph