単純なラジオ フィールドがあり、validate_on_submit が常に false を返します。form.errors を出力すると、coerce=int にもかかわらず、ラジオ フィールドからの値として「有効な選択ではありません」が渡されているように見えます。
フォームで返されたものを壊しているとは思いません。うまくいけば、動的な選択を正しい方法で作成できます。これが失敗する理由がわかりません。
これが私のプロジェクトの関連部分です - どんな提案も大歓迎です。
フォーム.py:
class SelectRecord(Form):
rid = RadioField("Record Select", choices=[], coerce=int,validators=[InputRequired()])
ビュー.py:
@mod.route('/select/', methods=('GET', 'POST'))
@login_required
def select_view():
form = SelectRecord(request.form)
if form.validate_on_submit():
rid = form.data.rid
if form['btn'] == "checkout":
# check out the requested record
Records.checkoutRecord(rid)
return render_template('/records/edit.html',rid=rid)
elif form['btn'] == "checkin":
Records.checkinRecord(rid)
flash("Record checked in.")
else:
mychoices = []
recs_co = session.query(Records.id).filter(Records.editing_uid == current_user.id). \
filter(Records.locked == True)
for x in recs_co:
mychoices.append((x.rid,"%s: %s (%s)" % (x.a, x.b, x.c, x.d)))
x = getNextRecord()
mychoices.append((x.id,"%s: %s (%s %s)" % (x.a, x.b, x.c, x.d)))
form.rid.choices = mychoices
print form.errors
return render_template('records/select.html', form=form)
そして私のテンプレート(select.html):
<form method="POST" action="/select/" class="form form-horizontal" name="select_view">
<h1>Select a record to edit:</h1>
{{ render_field(form.rid, class="form-control") }}
{{ form.hidden_tag() }}
<button type="submit" name="btn" class="btn" value="Check Out">Check Out</button>
<button type="submit" name="btn" class="btn" value="Check In">Check In</button>
</form>