jeudi 2 octobre 2014

wtforms data different from request.form


Vote count:

0




I have a simple Flask-WTForm that contains a WTForm Fieldlist. The definitions are as follows:



class ColorField(wtforms.Form):
lower = IntegerField('Price 1')
upper = IntegerField('Price 2')
color = StringField('Color', validators=[Length(6,6)])

class ColorForm(Form):
h_id = HiddenField()
entries = FieldList(FormField(ColorField), min_entries=1)
submit = SubmitField('Save')


In my view I populate the form with database entries (PriceColor is my model - the prefix is because I have 2 forms on the same page - I followed the trick here):



colorform = ColorForm(h_id=h_id, prefix="color")
hpc = PriceColor.query.filter_by(h_id=h_id).all()
if len(hpc)>0:
colorform.entries.pop_entry()
for e in hpc:
colorform.entries.append_entry(e)
db.session.delete(e)


This is a form with a variable number of fields - users can add or remove rows in the form before submitting (using javascript). The idea is that I read everything from the database, populate the form, remove all current data from the database, then write back in new data as provided by the submitted form.


The problem is colorform.data doesn't change even after user modifies the form (so it's the same as when the form was first populated), so validate_on_submit() is useless. I checked request.form and it is correct, so how can I get Flask-WTForms to validate that instead?



asked 39 secs ago







wtforms data different from request.form

Aucun commentaire:

Enregistrer un commentaire