You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We want to make a form with an input for each item so that we can change all the percentages in one go and check they still add up to 100%. The number of items will change over time based on what's in our database, so we can't hard-code them as class-level attributes and need to specify the fields dynamically instead.
classDynamicFieldForm(Form):
# item_1 = InputField(item_1_id) # can't do this# code to do validation, etc.passform=DynamicFieldForm(
extra_fields=[
InputField(item['id']) foriteminitems
],
data={
item['id']: item['percentage'] foriteminitems
}
)
Actual Behavior
It's possible to pass fields in the constructor with this hack:
# toy example without validationclassDynamicFieldForm(Form):
def__init__(self, items):
self._unbound_fields= [
(item['id'], InputField(item['id'])
foriteminitems
]
super().__init__(data={
item['id']: item['percentage'] foriteminitems
})
...
# get data for an item later ongetattr(form, item['id']).data
Using the internal _unbound_fields attribute isn't great as it's not part of the public interface and might break in future versions. Would it be possible to expose it in the constructor for the Form class?
Environment
Python version: 3.9
wtforms version: 3.0.1
The text was updated successfully, but these errors were encountered:
benthorner
pushed a commit
to alphagov/notifications-admin
that referenced
this issue
Apr 6, 2022
This replaces the slider with an integer input for each provider.
Unfortunately showing a variable number of inputs isn't easy to
achieve in WTForms [^1], but we think this is the least worst way
to do it vs e.g. not using WTForms at all.
[^1]: pallets-eco/wtforms#736
benthorner
pushed a commit
to alphagov/notifications-admin
that referenced
this issue
Apr 7, 2022
This replaces the slider with an integer input for each provider.
Unfortunately showing a variable number of inputs isn't easy to
achieve in WTForms [^1], but we think this is the least worst way
to do it vs e.g. not using WTForms at all.
[^1]: pallets-eco/wtforms#736
benthorner
pushed a commit
to alphagov/notifications-admin
that referenced
this issue
Apr 7, 2022
This replaces the slider with an integer input for each provider.
Unfortunately showing a variable number of inputs isn't easy to
achieve in WTForms [^1], but we think this is the least worst way
to do it vs e.g. not using WTForms at all.
[^1]: pallets-eco/wtforms#736
benthorner
pushed a commit
to alphagov/notifications-admin
that referenced
this issue
Apr 7, 2022
This replaces the slider with an integer input for each provider.
Unfortunately showing a variable number of inputs isn't easy to
achieve in WTForms [^1], but we think this is the least worst way
to do it vs e.g. not using WTForms at all.
[^1]: pallets-eco/wtforms#736
Expected Behavior
We have some data like the following:
We want to make a form with an input for each item so that we can change all the percentages in one go and check they still add up to 100%. The number of items will change over time based on what's in our database, so we can't hard-code them as class-level attributes and need to specify the fields dynamically instead.
In theory we could do this with the BaseForm class, passing an unbound field for each input in the constructor. However, this isn't possible for us in practice as we're using the FlaskForm class from flask_wtf, which inherits from Form. The Form class locks the set of fields to the class-level attributes discovered by its metaclass.
Ideally we would do this:
Actual Behavior
It's possible to pass fields in the constructor with this hack:
Using the internal
_unbound_fields
attribute isn't great as it's not part of the public interface and might break in future versions. Would it be possible to expose it in the constructor for the Form class?Environment
The text was updated successfully, but these errors were encountered: