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

Cannot update field data on numeric fields when formdata is processed #834

Open
tspng opened this issue Feb 5, 2024 · 1 comment
Open

Comments

@tspng
Copy link

tspng commented Feb 5, 2024

I think I encountered a bug related to how field values are rendered on numeric fields.
I am trying to dynamically change some field values after the form has been submitted. It works on StringFields for example but not on IntegerFields.

Actual Behavior

from werkzeug.datastructures import ImmutableMultiDict
from wtforms import Form, IntegerField, StringField


class MyForm(Form):
    string_field = StringField("Name")
    integer_field = IntegerField("Value")


formdata = ImmutableMultiDict([("string_field", "World"), ("integer_field", "")])
form = MyForm(formdata=formdata)
assert form.string_field.data == "World"
assert form.integer_field.data is None

form.string_field.data = "Hello"
assert form.string_field.data == "Hello"
assert 'value="Hello"' in form.string_field()  # <- works

form.integer_field.data = 42
assert form.integer_field.data == 42
assert 'value="42"' in form.integer_field()  # <- AssertionError
Traceback (most recent call last):
  File "/tmp/test.py", line 21, in <module>
    assert 'value="42"' in form.integer_field()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Expected Behavior

Allow setting the field data on numeric fields after formdata is processed.
So the behaviour is aligned with other non-numeric fields.

Problem

Right now, all (or most) numeric fields use raw_data[0] if present for the fields value in the rendered HTML output.

def _value(self):
    if self.raw_data:
        return self.raw_data[0]
    if self.data is not None:
        return str(self.data)
    return ""

Environment

  • wtforms version: 3.0.1

EDIT: typo

@tspng
Copy link
Author

tspng commented Feb 5, 2024

I just saw that some of that behaviour has been previously discussed (#662). I can understand the rationale that by default the user input should be displayed as entered when the form is shown again.

But as far as I know, the field's value can't be changed if the field is re-rendered. Is that correct?

My (ugly) workaround is to set field.raw_data = None as well as field.data = <NEW VALUE>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant