Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing FormField seperator parameter to non-default breaks iteration over enclosing FieldList #681

Closed
bah235 opened this issue Feb 18, 2021 · 1 comment · Fixed by #694
Labels
bug Unexpected behavior

Comments

@bah235
Copy link

bah235 commented Feb 18, 2021

I have a use case for generating a FieldList of a variable number of simple forms each enclosed in a FormField. I have attempted setting the separator parameter of the FormField. Changing FormField separator parameter to non-default (aka from "-" to "_" breaks iteration over enclosing FieldList. Only one entry is returned regardless of how many are in the list.

Actual Behavior

Vastly simplified code below. Eveything works as intended if you leave separator set as default "-" but breaks if changed to "_"

# Form for individual person's test score
class OneScoreForm(Form):
    Score = DecimalRangeField('Score',default = 1)

# List of forms for entering man people's test scores
class ScoringForm(Form):

    Scores = FieldList(FormField(OneScoreForm,  separator='_'), min_entries=1)
    submit = SubmitField('Submit Your Scores')

form = ScoringForm()

# Build Form
for person in people:
    form.Scores.append_entry()
    form.Scores[-1]['Person'].data = person.Person

# Render some HTML
# Get form data back with imputted scores

# Try to iterate over people and write to database
for score in form.Scores:
    # Put form data back into database

This returns only the first entry regardless of how many Score forms are appended to the list.

Expected Behavior

Iterates over the whole list of submitted forms regardless of setting separator parameter.

Additional Info

I do have a fix in my code that works by setting the hard coded use of the hyphens in FieldList._add_entry and FieldList._extract_indices to instead use the specified separator. It does pass all the package tests but I still don't feel confident it will break something else.

The reason setting the separator matters to me is that I would like to eliminate the hyphens from he field names so I can use a bit of java on them to display the value of the range slider in real time. Hyphens are forbidden.

Environment

  • Python version: 3.8.5
  • wtforms version: 2.3.3
@azmeuk
Copy link
Member

azmeuk commented May 19, 2021

I could reproduce the bug:

Snippet working with hyphens:

>>> import wtforms
...
... class DummyPostData(dict):
...     def getlist(self, key):
...         v = self[key]
...         if not isinstance(v, (list, tuple)):
...             v = [v]
...         return v
...         
... class InnerForm(wtforms.Form):
...     innerfield = wtforms.StringField()
...     
... class OutterForm(wtforms.Form):
...     forms = wtforms.FieldList(wtforms.FormField(InnerForm), min_entries=1)
...
...
... formdata = DummyPostData({"forms-0-innerfield": "foo", "forms-1-innerfield": "bar"})
... form = OutterForm(formdata)
... for innerform in form.forms:
...     print(innerform.innerfield.data)
foo
bar

Snippet not working with underscores:

>>> import wtforms
...
... class DummyPostData(dict):
...     def getlist(self, key):
...         v = self[key]
...         if not isinstance(v, (list, tuple)):
...             v = [v]
...         return v
...         
... class InnerForm(wtforms.Form):
...     innerfield = wtforms.StringField()
...     
... class OutterForm(wtforms.Form):
...     forms = wtforms.FieldList(wtforms.FormField(InnerForm,  separator='_'), min_entries=1)
...
...
... formdata = DummyPostData({"forms-0_innerfield": "foo", "forms-1_innerfield": "bar"})
... form = OutterForm(formdata)
... for innerform in form.forms:
...     print(innerform.innerfield.data)
foo

@azmeuk azmeuk added the bug Unexpected behavior label May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected behavior
Development

Successfully merging a pull request may close this issue.

2 participants