In my form I am trying to create a custom array field with choices.
The custom form field:
class CustomField(Field):
widget = TextInput()
def _value(self):
if self.data:
return u', '.join(self.data)
else:
return u''
def process_formdata(self, valuelist):
if valuelist:
self.data = [x.strip() for x in valuelist[0].split(',')]
else:
self.data = []
The actual form calls the custom form field
class PostForm(Form):
status = CustomField()
Whenever, I post data to PostForm
it calls the custom field but does not pass in an any valuelist
to process_formatdata
. The custom field always returns a empty list.
Am I missing any thing here.
Your code works for me, here's a working example--
from flask import Flask, render_template
from flask.ext.wtf import Form, SubmitField, Field, TextInput
app = Flask(__name__)
app.config['SECRET_KEY'] = 'Shh!'
class CustomField(Field):
widget = TextInput()
def _value(self):
if self.data:
return u', '.join(self.data)
else:
return u''
def process_formdata(self, valuelist):
if valuelist:
self.data = [x.strip() for x in valuelist[0].split(',')]
else:
self.data = []
class ExampleForm(Form):
status = CustomField()
submit = SubmitField('POST')
@app.route('/', methods=['post', 'get'])
def hello_world():
form = ExampleForm()
if form.validate_on_submit():
for entry in form.status.data:
print '{}'.format(entry)
return render_template('test.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
<form action="." method="post">
{{ form.hidden_tag() }}
{{ form.status }}
{{ form.submit }}
</form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With